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

js var vs let

let = 10 // accessable only in defined scope
var = 10 // accessable in the function it is declared in (global variable in a function)
  
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

what to use let vs var js

/* DIFFERENCE BETWEEN LET AND VAR */

//LET EXAMPLE
{
  let a = 123;
};

console.log(a); // undefined

//VAR EXAMPLE
{
  var a = 123;
};

console.log(a); // 123
Comment

difference between "let" and "var"?

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

PREVIOUS NEXT
Code Example
Javascript :: how to convert object to array in javascript 
Javascript :: hostlistener 
Javascript :: vuejs delay watch 
Javascript :: remove id from array javascript 
Javascript :: reverse string in javascript 
Javascript :: javascript regex grouping replace 
Javascript :: how to count seconds in javascript 
Javascript :: updating json object in mysql database 
Javascript :: javascript clone object 
Javascript :: JavaScript POSITIVE_INFINITY 
Javascript :: open ai gym 
Javascript :: shadow class angular 
Javascript :: id button click jquery 
Javascript :: mongodb insertmany 
Javascript :: compare date javascript 
Javascript :: array.flat 
Javascript :: (node:9130) electron: Failed to load URL: http://localhost:5000 
Javascript :: reactjs lifecycle class components 
Javascript :: change image automaticly 
Javascript :: using datatable 
Javascript :: append string javascript 
Javascript :: connect to existing collection mongoose 
Javascript :: jest cannot find module 
Javascript :: range number in js 
Javascript :: append string js 
Javascript :: what is a block in javascript 
Javascript :: javascript add query string to url 
Javascript :: lodash remove not in array 
Javascript :: moment iso string 
Javascript :: find object and remove in array 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =