Answers for "difference between var and let in javascript"

19

var vs let js

let: //only available inside the scope it's declared, like in "for" loop, 
var: //accessed outside the loop "for"
Posted by: Guest on May-22-2020
10

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.
Posted by: Guest on November-27-2020
4

difference between "let" and "var"?

In simple words 'var' is function scoped and 'let' is block scoped
Posted by: Guest on October-02-2021
3

Difference between let and var in javascript

let a = 'hello'; // globally scoped
var b = 'world'; // globally scoped
console.log(window.a); // undefined
console.log(window.b); // 'world'
var a = 'hello';
var a = 'world'; // No problem, 'hello' is replaced.
let b = 'hello';
let b = 'world'; // SyntaxError: Identifier 'b' has already been declared
Posted by: Guest on May-22-2021
12

let in javascript

The let statement declares a block scope local variable, optionally initializing it to a value.

let x = 1;

if (x === 1) {
  let x = 2;

  console.log(x);
  // expected output: 2
}

console.log(x);
// expected output: 1
Posted by: Guest on April-02-2020
0

difference between var and let in javascript

/*

Therefore, 
  
  var:
    -can be declared outside any function to be used inside any function
    -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
    -can be changed again and again anywhere
  let:
    -can be declared outside any function to be used inside any function
    -if declared inside any function or any other {} which are of only if or if-else or switch etc. and can't be used anywhere inside the function and can be only used inside statement
    - can be changed again and again only inside the statement in which they are made in
    const:
    -can be declared outside any function to be used inside any function
    -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
    -cannot be changed again and agan anywhere, if tried to, will result in an error

*/
// var has function scope. (this variable can be accssed from anywhere inside function)
// let & const has block scope.(this variable can not be accessed out of block. means outside of if else but inside of a function it can not be accessed. only inside that block we can access this variable)
// https://www.youtube.com/watch?v=--Vh17ocC_s
function func(){
  if(true){
    var A = 1;
    let B = 2;
  }
  A++; // 2 --> ok, inside function scope
  B++; // B is not defined --> not ok, outside of block scope
  return A + B; // NaN --> B is not defined
}
//example 2
var variable1 // declared using var
const variable2 // declared using const

myFunction1();

function myFunction1() {
  variable1 = "hello!";
  console.log(variable1);
  // "hello!"
}

myFunction2();

function myFunction2() {
  variable2 = "hello!";
  // error: variable2 is a constant and can not be redifined
}

myFunction3(true);

myFunction3(codition) {
  if(condition) {
    let variable3 = "helo!" // declared using const
  }
  variable3 = "hello!";
  // error: variable3 is out of scope
}
Posted by: Guest on October-07-2021

Code answers related to "difference between var and let in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language