Node JS

var,let,const difference

This tutorial will teach you the main difference between var,let,const step by step.

var let const difference

1.var is global scope.

2. let is block scope.

3.const is block scope.

variable declaration

var mytest1 = 12;
let mytest2 = "javascript";
const mytest3 = 2000;

let we discuss about each

var

var is global scope.

var mytest = 12;
console.log(mytest);

Output

12

i created the variable name mytest assign 12. you can see the output.

console.log(mytest);
var mytest = 12;
var mytest = 120;
var mytest = 12000;
var mytest = 0;
console.log(mytest);

Output

undefined
0

above example i printed console.log(mytest); at the first time. no value assign thats way output display as undefined. same mytest name you can assign more than one value but when you print it out  last declared value 0 as been printed. so that output displayed as 0.

console.log(mytests);

if(1000 > 500)
{
    var mytests = 509;
   
}
console.log(mytests);

Output

undefined
509

above example console.log(mytests); at the first time. no value assign thats way output display as undefined.
after that if the particular condition correct only print the value 509. 1000 is grater than 500 condition is corrent then print it out 509.

let

let is block scope.

let mytestvar = 120;
console.log(mytestvar);

Output

120

console.log(mytestvar);
let mytestvar = 120;
console.log(mytestvar);

Error

ReferenceError: Cannot access ‘mytestvar’ before initialization

you cannot call console.log(mytestvar); at the firstline you get the error.

let mytest = 12;
let mytest = 120;
let mytest = 12000;
let mytest = 0;
console.log(mytest);

Error

SyntaxError: Identifier ‘mytest’ has already been declared

Same name you cannot use again by again.

let myTestlett = 0; // you must create the variable first at the firstline then only works.

if(1000 > 10)
{
     mytestlet = 7000;
}
console.log(mytestlet);

you must create the variable first at the firstline then only works because let is a block scope

Output as

7000

const

const mytest = 12;

console.log(mytest);

Output

12

const mytest = 12;
const mytest = 120;
const mytest = 12000;
const mytest = 0;
console.log(mytest);

Error

SyntaxError: Identifier ‘mytest’ has already been declared.

Same name you cannot use again by again.

const mytest = 200;

mytest = 1200;

console.log(mytest);

Error

TypeError: Assignment to constant variable.

when we create a const variable value we can assign once cannot assign again
const myestarray = {name : "Tutusfunny", age: 300};

console.log(myestarray.name);


const myestarray1 = {name : "Tutusfunny", age: 300};
myestarray.name = "Tutusfunny123";

console.log(myestarray1.name);

const myestarray2 = [12,20,50];
myestarray2[0] = 60;

console.log(myestarray2);

Output

Tutusfunny
Tutusfunny
[ 60, 20, 50 ]

 

 

 

 

 

 

 

 

 

admin

Recent Posts

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…

2 weeks ago

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

1 month ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

1 month ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

2 months ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

2 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago