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 "let" and "var"?

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

PREVIOUS NEXT
Code Example
Javascript :: node js download file to folder 
Javascript :: jquery form data 
Javascript :: redirect in vue 
Javascript :: export all functions 
Javascript :: react hook form reset 
Javascript :: difference between let and var in javascript 
Javascript :: how to get text from input js 
Javascript :: react native npm run start port 
Javascript :: how to implement redis pub sub model using nodejs 
Javascript :: create a binary tree 
Javascript :: js generate random string of length 
Javascript :: emailjs react npm 
Javascript :: jquery make visible 
Javascript :: js create array from for loop 
Javascript :: react select with custom option 
Javascript :: sticky footer react 
Javascript :: object json parse javascript 
Javascript :: suspense react 
Javascript :: if else dart 
Javascript :: angular disable click 
Javascript :: js list of objects 
Javascript :: how to get thumbnail image from video file in javascript 
Javascript :: create angular project 
Javascript :: get query parameters in node.js 
Javascript :: get the last day of the month in js 
Javascript :: input focus in jquery 
Javascript :: fromcharcode in javascript 
Javascript :: js assignment operators 
Javascript :: click counter in javascript 
Javascript :: get date one week from now javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =