variables in JavaScript(let,var,const) ๐Ÿ’™

variables in JavaScript(let,var,const) ๐Ÿ’™

Table of contents

ยท

3 min read

  1. Introduction of Variables in JavaScript

  2. Intro about let

  3. Intro about var

  4. Intro about const

All about Variables Lets go๐Ÿ˜

Variables are the building blocks of any programming language, used to store and manipulate data. They serve as containers that hold values that can be referenced and changed throughout your code. variables are a container for a value, like a number we might use in a sum, or a string that we might use as part of a sentence.

Variables aren't the values themselves basically they are containers for values. You can think of them being like little cardboard boxes in above example that you can store things in. you can store any type of datatypes in any variables just need to be in single inverted comma in case of string, you can check type of datatypes using "typeof" function.

let b = 3;
const c = 5.0;
var name = 'pushkar';
var age = 22;

console.log( age ); // 22
typeof(age) //number
console.log( b ); // 3
typeof(b) //number
console.log( c );//5
typeof(c) //number
console.log( name );//pushkar
typeof(name) //string

Intro to let

let variable are basically blocked scoped variable that means we can not accessible outside block we can see example below. The let declaration declares re-assignable, block-scoped local variables, optionally initializing each to a value. variables has the block scope. It can not be accessible outside the particular code block ({block}). block are generally range of any statements or any function code. we declare it with let keyword. The scope of a variable declared with let curly-brace-enclosed syntaxes that most closely contains the let declaration. we can not redeclare another variable with same name.

let a = 2;
if (a > 1) {
let b = a * 3;
console.log( b ); // 6
for (let i = a; i <= b; i++) {
let j = i + 10
console.log( j );
}
// 12 13 14 15 16
let c = a + b;
console.log( c ); // 8
}

//Example 2
{
console.log( a ); // undefined
console.log( b ); // ReferenceError : b is not declared!
var a;
let b;
}
{
  let foo;
  let foo; // SyntaxError: Identifier 'foo' has already been declared
}

Intro to var

The var statement declares function-scoped or globally-scoped variables, optionally initializing each to a value. The var is the oldest keyword to declare a variable in JavaScript. it has the global scoped or function scoped which means variables defined outside the function can be accessed globally, and variables defined inside a particular function can be accessed within the function.

In var we can :-

re-declare variable.

re-initialized.

user can update.


var a = 10

// User can re-declare
// variable using var
var a = 8

// User can update var variable
a = 7 
console.log(a); // 7
var a = 2;
(function IIFE(){
var a = 3;
console.log( a ); // 3
})();
console.log(a); // 2

Intro to const

The const declaration declares block-scoped local variables. The value of a constant can't be changed through reassignment using the assignment operators but if a constant is an object, its properties can be added, updated, or removed.Itโ€™s a variable thatโ€™s read-only after its initial value is set.

The const declaration is very similar to let:

  • const declarations are scoped to blocks as well as functions.

  • const declarations can only be accessed after the place of declaration.

const a = 10;
function f() {
    a = 9
    console.log(a)
}
f(); //TypeError:Assignment to constant variable.
{
const a = [1,2,3];
a.push( 4 );
console.log( a ); // [1,2,3,4]
a = 42; // TypeError!
}

Note: Sometimes, users face problems while working with the var variable as they change its value of it in a particular block. So, users should use the let and const keywords to declare a variable in JavaScript.

Happy coding ๐Ÿ’™

ย