JavaScript Variables

JavaScript Variables

Var, Let and Const

What are Variables?

In general programming premise, Variables are the names you give to computer memory locations which are used to store values in a computer program. and these values may or may not change.

Variables in JavaScript are containers which hold reusable data/values. They are like containers, they can be filled with stuff, and this stuff can be used over and over again depending on whichever way we choose. In some cases, the stuffs can be removed and replaced.

Declaring Variable in JavaScript

Before ES2015 (ES6), JavaScript variables were solely declared using the var keyword.

In ES6, two other ways to declare variables were introduced, they are the let and const .

VAR

/*Declaring variable and assigning value to it*/
var Name = 'Godwin Martins';
console.log(Name);

The above code snippet is going to take the string value 'Godwin Martins' and save it into the variable/container 'Name' for later use.

It is also possible to reassign value to the variable, that means, overwriting the previous data stored on the container.

/* Reassigning value to initially declared variable with a different value entirely*/
Name = "Godwin Philip";
console.log(Name);

Again, it is possible to just to just declare variable without assigning any value to it, for example

/* Just init Var*/
var greetings;
console.log(greetings);

Note: The output of this code will be undefined since there's no value assigned to greetings.

LET

Introduced in ES6, the variable type let shares a lot of similarities with var but unlike var has scope constraints. let is constrained to whichever scope it is declared in. Its declaration and assignment are similar to var.

let was introduced to mitigate issues posed by variables scope which developers face during development.

/* Using LET */
let SName = 'Abu Godwin';
console.log(SName);
SName = 'Akhalumhe';
console.log(SName);

Basically, what happened is that you can declare a variable with or without assigning a value to it, then you can later assign a value in the cause of the program. In this case, the value of the variable is liable to change.

CONST

It actually stands for constant which simply means it can't change or cannot be reassigned after the initial assignment Note: You must assign a value on initialization, otherwise it'll flag an error.

/* Using LET */
const dyor = 'Do Your own Research';
console.log(dyor);

In this case, dyor remains unchanged throughout the program.

On Arrays and Object, when declared with const, you can mutate the array or object/ change properties, but you can't change to entire Object or Array (more on this later).

##NAMING CONVENTIONS FOR VARIABLES

Variable names can contain letters (a-z), numbers(0-9), Underscore (_) and Dollar sign ($). Variable Name cannot start with a number and cannot contain symbols such as !"£%^&&**()))+~}{... etc.

Multi-Words Variables Conventions

Camel Case

In this case, the first word starts with a small letter while the second word starts with a capital letter. example

var firstName = "John";

Underscore

In this case, the words are combined with an underscore separating them. example

var first_name = "john";

Pascal case

Here, first letter of each word is an Upper Case letter. Example

var FirstNmae = "Tom";

Lowercase

As the name implies all characters of both words are in lower case. Example

const pi = 3.14;

Conclusion

  • Variable stores a single data value that can be changed later except constants.

  • Variables can be defined using var, let and const keyword. Variables defined with let or const keyword become global variables.

  • Variables must be initialized before using (Otherwise you get undefined as a return value.
  • Variables in JavaScript are loosely-typed variables. It can store value of any data type through out it's life time (More on this on a later Article).