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

var vs let javascript

var makes a grobal variable
let makes a local variable
Comment

difference between "let" and "var"?

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

PREVIOUS NEXT
Code Example
Javascript :: find last element in array nodejs 
Javascript :: vue cors 
Javascript :: react state array 
Javascript :: angular ngmodel checkbox 
Javascript :: set cookie and get cookie in javascript 
Javascript :: npm execute script with nodemon 
Javascript :: javascript calculate percentage to pixel 
Javascript :: javascript print path 
Javascript :: Group array of strings by first letter 
Javascript :: react prevstate 
Javascript :: How to fetch API data using POST and GET in PHP 
Javascript :: how to capitalize first letter in javascript 
Javascript :: js pad array 
Javascript :: jquery submit form 
Javascript :: hashing in node js 
Javascript :: delete node module 
Javascript :: How to replace a value in localstorage using javascript 
Javascript :: javascript type casting int 
Javascript :: divisible by 3 javascript 
Javascript :: continuos scrollling js 
Javascript :: round decimal js 
Javascript :: javascript stop execution 
Javascript :: get query parameters in node.js 
Javascript :: pass params axios get react 
Javascript :: array.unshift in javascript 
Javascript :: array map arrow function 
Javascript :: next router 
Javascript :: tinymce update textarea value using jquery 
Javascript :: count down timer in react native 
Javascript :: change key name in array of objects javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =