var vs let js
let: //only available inside the scope it's declared, like in "for" loop,
var: //accessed outside the loop "for"
var vs let js
let: //only available inside the scope it's declared, like in "for" loop,
var: //accessed outside the loop "for"
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
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.
}
let javascript
* Variables defined with let cannot be redeclared.
* You cannot accidentally redeclare a variable.
let x = "John Doe";
let x = 0;
// SyntaxError: 'x' has already been declared
let javascript
The let statement declares a block-scoped local variable,
optionally initializing it to a value.
let x=1;
let javascript
var i = "global";
function foo() {
var i = "local"; // Otra variable local solo para esta función
console.log(i); // local
}
foo();
console.log(i); // global
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us