techStackGuru

JavaScript Variables


A variable in JavaScript is a storage space for values. The "var", "let," or "const" keywords can be used to declare variables, which can store a variety of data types, including numbers, strings, arrays, objects, etc. Variables are used to store and alter data in scripts and programs. The value saved in a variable can be modified at any moment.

When declaring a JavaScript variable, there are some guidelines to follow (also known as identifiers).

  • Name must start with a letter ( A to Z or a to z), dollar( $ ) or underscore( _ )sign.
  • Variables in JavaScript are case sensitive, so v and V are two separate variables.

Lets take an example

var name = "Vishal";

// or
var name;
name = "Vishal"; 

Lets see another variable

var x = 2;  
document.write(x); 

Local Variable

A local variable in JavaScript is declared within a block or function. It can only be accessed from within the function or block. For example

function demo(){  
  var z=20;  //local variable  
}  

Global Variable

You can access global JavaScript variables from any function. Variables are declared outside the function. For example

var name= "vishal"; //global variable  

function printname(){  
  document.writeln(name);  
}  

printname(); 

You can declare variable only once.

let name = "Midhun";
let name = "Anu"; 

// Output
let name = "Anu"; 
    ^
SyntaxError: Identifier 'name' has already been declared

"const" keyword

The "const" keyword is used to declare constant variables in JavaScript. Once a variables value has been assigned, it cannot be modified, making it a constant variable. A constant variables value must be determined at the moment of declaration and cannot be altered afterwards.

const PI = 3.14;
console.log(PI); // Output: 3.14

PI = 3.14159; // Error: Assignment to constant variable.

You can ensure immutability and avoid problems and unintentional modifications in your code by using the keyword "const." Its crucial to remember that even while the variable itself is constant, the value that is applied to it can still change (e.g. objects and arrays).

Reserved names

Some keywords cannot be used as variable names. Example

let let = 9; // can't name a variable "let", error!
    ^^^
SyntaxError: let is disallowed as a lexically bound name

Comparing Values

let val1 = 2
let val2 = 2
console.log(val1===val2) // true

let val1 = 2
let val2 = 3
console.log(val1===val2) // false

let val1 = "hello"
let val2 = "hello"
console.log(val1===val2) // true

let val1 = "hello"
let val2 = "aura"
console.log(val1===val2) // false 

Difference between var, let and const keyword

Scope

  • "var" variables are available both within the function in which they are declared and any nested functions since they have function scope.
  • The variables "let" and "const" only have access within the block in which they are defined and any nested blocks since they have a block scope.
  • Re-declaration

  • Even with the same name, "var" variables can be declared again within the same scope.
  • "let" cannot be declared again while still in the same block scope, although they can be given a new value.
  • "const" variables cannot be re-declared or re-assigned.
  • Mutation

  • After being declared, "var" variables can be modified.
  • After being declared, "let" variables can be modified.
  • After being declared, "const" variables cannot be altered (mutated).

  • previous-button
    next-button