Skip to Main Content
Rediscovering JavaScript
book

Rediscovering JavaScript

by Venkat Subramaniam
June 2018
Beginner content levelBeginner
288 pages
6h 31m
English
Pragmatic Bookshelf
Content preview from Rediscovering JavaScript

const

The const keyword is used to define a variable whose value shouldn’t change. If you intend to modify the value in a variable, then define it using let; otherwise, define it using const.

Here’s an example that shows the difference between using let and const:

 //BROKEN CODE
 'use strict'​;
 let​ price = 120.25;
 const​ tax = 0.825;
 
 price = 110.12;
 
 tax = 1.25;

There’s no issue changing the value of the price variable. However, since tax is defined as a constant, we will get a runtime error when we try to modify the value:

 tax = 1.25;
  ^
 
 TypeError: Assignment to constant variable.

Reach of const

Before we declare const as one of the most awesome features in modern JavaScript, let’s understand its limitations. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Object-Oriented JavaScript - Third Edition

Object-Oriented JavaScript - Third Edition

Stoyan STEFANOV, Ved Antani
Deno Web Development

Deno Web Development

Alexandre Portela dos Santos

Publisher Resources

ISBN: 9781680506174Errata Page