Skip to content

Instantly share code, notes, and snippets.

@shreshthmohan
Created February 10, 2018 16:56
Show Gist options
  • Save shreshthmohan/3e534337c9ed35ea48aad4a7b70b0554 to your computer and use it in GitHub Desktop.
Save shreshthmohan/3e534337c9ed35ea48aad4a7b70b0554 to your computer and use it in GitHub Desktop.
ES6 - let - block scoping // source http://jsbin.com/vahifug
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>ES6 - let - block scoping</title>
</head>
<body>
<script id="jsbin-javascript">
const a = 6;
const b = 7;
if (a < b ) {
let c = 8;
console.log('c from inside the if block: ', c);
}
try {
console.log('c from outside the if block: ')
console.log(c);
} catch(err) {
console.log(err);
}
console.log('See that? This is block scoping. The variable c is available only inside the block it\'s declared in');
</script>
<script id="jsbin-source-javascript" type="text/javascript">
const a = 6;
const b = 7;
if (a < b ) {
let c = 8;
console.log('c from inside the if block: ', c);
}
try {
console.log('c from outside the if block: ')
console.log(c);
} catch(err) {
console.log(err);
}
console.log('See that? This is block scoping. The variable c is available only inside the block it\'s declared in');
</script></body>
</html>
const a = 6;
const b = 7;
if (a < b ) {
let c = 8;
console.log('c from inside the if block: ', c);
}
try {
console.log('c from outside the if block: ')
console.log(c);
} catch(err) {
console.log(err);
}
console.log('See that? This is block scoping. The variable c is available only inside the block it\'s declared in');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment