ES6 in Depth
  • Introduction
  • Installation
  • Block Scope
    • Constant
    • Use cases
  • Template String
    • New String Methods
    • Tagged Template Literal
  • New Number Methods
  • Arrow Function
    • Default Parameters
    • Lexical This Scope
    • Where not to use arrow function
  • Object Enhancement
  • New Array Methods
    • For Of
  • Spread Operator
  • Destructuring
    • Array Destructuring
  • Class
  • Symbols
  • New Data Structures
    • Set
    • Map
    • WeakSet
    • WeakMap
    • Iterators
    • Generators
  • Promise
  • Import Export
Powered by GitBook
On this page
  • Another Example
  • Use Cases

Was this helpful?

  1. Block Scope

Constant

have block scope like let keyword

// ES5
var value = "hey";
value = "Cool";
console.log(value); // Cool

// ES6
const value = "hey";
value = "Cool";
console.log(value); // Error: "value" is read-only

Another Example

const obj = {};
obj.name = "Hemant";
console.log(obj); // {name: "Hemant"}

// but
const obj = {};
obj = {location: "HYD"};
console.log(obj); // Error: "obj" is read-only

obj.location = "BLR"; 
console.log(obj.location); // BLR

// If you really want to frreeze
const obj = Object.freeze(obj);
obj.location = "BLR"; 
console.log(obj.location); // still "HYD"

Use Cases

const API_KEY = "XXX";
const API_SECRET = "DDGD_DDDGG-SS";
const port = 4000;
const PI =3.14;
PreviousBlock ScopeNextUse cases

Last updated 4 years ago

Was this helpful?