Answers for "var and let in javascript"

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
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
1

var and let in javascript

Var {
  Before the advent of ES6,
  var declarations were used to declare a variable. 
  The properties of var is that it has visibility linked
  to the function to which it belongs. We can change its value,
  and it can be redeclared in the same scope. 
  Scope means where these variables are available for use.
  There are two types of scope, local and global. 
  Var declarations are globally scoped, 
  and when they are defined inside a function, they are locally scoped.
    }
let {
  The variable type let is introduced in ES6.
     It shares a lot of similarities with var,
     but unlike var, it has scope constraints. 
     Its declaration and assignment are similar to var. 
     The purpose of introducing let is to resolve all issues 
     posed by variables scope, which developers face during development.
     The properties of let are that 
     They have visibility linked to the block they belong with.
     We can change their values, 
     but they cannot be redeclared in the same scope, unlike var.
}
Posted by: Guest on July-20-2021
-3

vars with let in it javascript

let /*Var name*/ = /*what is equals*/
Posted by: Guest on September-22-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language