Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

difference between var and let

var is function scoped and let is block scoped. Let's say you have:
function understanding_var() {
	if (1 == 1) {
    	var x = 5;
        console.log('the value of x inside the if statement is ' + x);
    }
    console.log(x);
} 
//output: the value of x inside the if statement is 5
		  5

function understanding_let() {
	if (1 == 1) {
    	let x = 5;
        console.log('the value of x inside the if statement is ' + x);
    }
    console.log(x);
} 
//output: the value of x inside the if statement is 5
		  Uncaught ReferenceError: x is not defined
          
var is defined throughout the entire function, even if it's inside the if 
statement, but the scope of let is always within the curly braces, not outside
it, even if the conditional statement is inside the function.
Comment

difference between let and var in javascript

//let
1. It's used in block-scoped.
2. It does not allow to redeclare variables.	
3. Hoisting does not occur in let.
// var 
1. It's used in function scoped.
2. It allows to redeclare variables.
3. Hoisting occurs in var.
Comment

difference let and var

------------------Differences------------------		var			let
Global Scope										Yes			No
Can be changed outside statement where made in		Yes			No
Block Scope											No			Yes
Comment

let and var difference

function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar);

  {
    let baz = "Bazz";
    console.log(baz);
  }

  console.log(baz); // ReferenceError
}

run();
Comment

difference between var, let, const

// var is a function scope ***
if(true){
    var varVariable = 'This is var';
    var varVariable = 'This is var again';
}

console.log(varVariable); // This is var again

// let is a block scope ***
if(true){
    let letVariable = 'This is let';
    let letVariable = 'This is let again';

    // let variable can't re-define but we can re-assign value


    console.log(letVariable); // let letVariable = 'This is let again';^SyntaxError: Identifier 'letVariable' has already been declared
}

console.log(letVariable); //ReferenceError: letVariable is not defined



// const variable can't re-define and re-assign value
// const is a block scope ***
if(true){
    const constVarible = {
        name: 'JavaScript',
        age: '25 years',
    };
    constVarible.name = 'JS';

    console.log(constVarible) // {name: 'JS',age: '25 years'} <= we can update const variable declared object 
}
Comment

difference between "let" and "var"?

In simple words 'var' is function scoped and 'let' is block scoped
Comment

PREVIOUS NEXT
Code Example
Javascript :: js arithmetic operators 
Javascript :: replace componentwillmount with hooks 
Javascript :: javascript collection to array 
Javascript :: export all functions from js file 
Javascript :: jsx classname multiple 
Javascript :: js different 
Javascript :: react state array 
Javascript :: force delete in sequelize 
Javascript :: node print variable 
Javascript :: data-id html javascript 
Javascript :: remove classname to node 
Javascript :: react prevstate 
Javascript :: bootstrap dropdown doesnt work with angular 12 
Javascript :: jquery is not defined rails 
Javascript :: discord client.send_message js 
Javascript :: document get elements by id js 
Javascript :: object json parse nestjs 
Javascript :: how to check platform in nodejs 
Javascript :: if else java 
Javascript :: column width table react 
Javascript :: js list pf objects 
Javascript :: To get thumbnail image from video file 
Javascript :: document.append 
Javascript :: node.js query parameters 
Javascript :: render markdown in nextjs 
Javascript :: set _id to id 
Javascript :: todashcase javascript 
Javascript :: javascript dynamic arrays 
Javascript :: click counter in js 
Javascript :: jquery chek radio 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =