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 :: process return value 
Javascript :: nodejs return value 
Javascript :: jquery data 
Javascript :: jquery select option value selected 
Javascript :: how to add lang attribute in next js 
Javascript :: react native firebase community template 
Javascript :: get javascript component position 
Javascript :: While resolving: gatsby-starter-ghost@2.0.0 npm ERR! Found: react@17.0.2 
Javascript :: react timer 
Javascript :: document.getelementbyid 
Javascript :: jquery once 
Javascript :: js string interpolation 
Javascript :: post request with headers 
Javascript :: sliding window algorithm javascript 
Javascript :: fetch json data into array 
Javascript :: regx to accept name 
Javascript :: array.slice 
Javascript :: how to create a website with javascript 
Javascript :: javascript import 
Javascript :: javascript window.onpopstate example 
Javascript :: javascript sort array of objects by value of key in object 
Javascript :: how to get class name of element in javascript 
Javascript :: context api react 
Javascript :: Javascript make alert box 
Javascript :: javascript foreach arrow function 
Javascript :: extract string from string javascript based on word 
Javascript :: convert exp date token to date 
Javascript :: postgres boolean column 
Javascript :: puppeteer event element change 
Javascript :: react fragment inside map with key prop 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =