/* Before ES6, blocks didn't really do anything to variables. Everything was global with var, and it still is, but let and const provide ways to declare and assign variables within the scope of a block. Let behaves mostly like var, but with scope. Const is an immutable constant, but because it's scoped, you can reuse the same constant in different blocks, and they won't interfere. */
JavaScript
function scopetest() {
let x = 10;
{
let x = 100;
}
{
let x = "This is a string even!";
}
return x;
}
console.log(scopetest());
//block and function scope
function fnScope() {
if(true) {
let j = '123';
j = '987';
const tr = 'mano';
console.log(j);
console.log(tr)
}
}
fnScope();
//Diff not defined and undefined
var a; // declaration part
a = 'mango'; //definition part
var b;
//console.log(z); // result is not defined, because its not defined and declared.
console.log(b)
//const - array is an object, so array can be modifiable, not re-assign
const arry = [3,5,2];
arry.push(8); // arry = [6,7,2]; not able to do this
console.log(arry)
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.