Variables in JavaScript.

Raj
The Dev Guide
3 min readOct 8, 2019

--

Most of the time, Javascript applications need to store data and work with the information we received from the user.

For Example —

  1. An Ecommerce Website, Where We can store user’s Cart, Wishlist, Goods being Sold and many more things.
  2. An Online Chatting App, where messages, users, and much more details are being stored.

Javascript variables are containers for storing Data values.

Javascript variables can be created using var, let, and const keywords. (Click Here to Learn Difference between these Keywords)

The Statement Below creates or declares a variable with the name message.

let message; // Declares a Variable

Now, you can put some data into your variable by using an assignment operator (“=”).

message = "Hello World!";

The string is now saved into the memory. We can access it using the variable name.

let message; 
message = "Hello World";
console.log(message) // "Hello World"

We can also declare and assign values to multiple variables at the same time.

let user = "John Doe", age = 27, message = "Hello World";console.log(user); // "John Doe"
console.log(age); // 27
console.log(message); // "Hello World"

In Older Scripts, you would often find the variable declaration using var instead of let i.e -

var message = "Hello World";

The var keyword is almost the same as let. It also declares a variable but in a slightly different, “old-school” way.

There are subtle differences between let and var which you can learn here.

Javascript Data Types

Javascript variables can store data as numbers such as 0, 1, 100, 1000, 1000.1213 and string values like “Hello World” , “Hey There! I am using Medium”.

In Javascript, strings can be written using double or Single Quotes. Numbers are written without quotes. If we write number using quotes it will be treated as String.

A Javascript variable assigned as String initially can also be modified as a number later without creating any error.

Variable Naming

There are few limitations to creating a variable name in Javascript —

  1. Name can contain letters, digits, underscores, and dollar signs.
let message; 
let message1;
let message_1;
let $message;

2. Name cannot begin with a digit.

let message1; // Valid 
let 1message; // Invalid;

3. Variable Name is case sensitive

let message = "Hello World";
let Message = "Hello People";

Here message and Message are not the same variables.

4. Reserve Words i.e Javascript cannot be used as variable names.

Talking about Naming, there’s one more important thing you should know.

A Variable name should be clean, have an obvious meaning and describe about the data that it stores.

Variable Naming is most complex and one of the most important skills in programming, having a quick look at variable name should describe most of the things about it and it can reveal much difference between a Beginner and an Experienced programmer.

In Real World Projects, most of the time is often utilized in studying and modifying codes rather than writing something from complete scratch. When we return to some code after spending our time doing something else it is much easier to find something that is well-labeled i.e something more descriptive.

Before declaring any variable spend some time thinking about an appropriate name for the variable to make it more descriptive.

Some Good-rules to Follow —

  1. Use more human-readable names such as costPrice or discountedPrice.
  2. Don’t use abbreviations such as a, b, c unless you are very sure about its meaning.
  3. Don’t use a variable name such as data or value, because it often confuses you and you might forget which data or value it is referring to.
  4. Have some discussion with your team members at times before starting any project. If on a Shopping website, products should be referred to as currentProduct or newProduct rather than product1 or product2.

This might sound easy while reading but creating a descriptive and concise variable is not easy it only comes after practice.

Summary

We can declare variables to store data using var, let and const keyword.

  • let is a modern variable declaration.
  • var is an old-school variable declaration. Normally we don’t use it at all.
  • const is like let, but the value of the variable can’t be changed.

--

--