javascript var vs 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.
Also in JavaScript:
- led brightness website nodemcu
- return object list in find js
- jquery links
- Javascript clear canvas
- javascript function that make a choice
- datepicker range npm reactjs
- node js http request get parameters
- every element in list after first javascript
- how to make an object in p5 go right to left with keycCicked p5
- img src in react js
- open pdf in new tab javascript
- js queuemicrotask
- jquery get request with headers
- react convert excel to json
- replace innerhtml javascript by regex
- react tooltip on disabled button
- random int from interval javascript
- javascript redirect to homepage
- largest and smallest number in an array 1-100 javascript
- javascript get stack trace
- javascript get element height
- slice()
- threejs
- validacao de campo vazio Toastr js