jskatas.org Continuously Learn JavaScript. Your Way.

Block scope: const declaration

const is like let plus read-only.

const is like let plus read-only

scalar values are read-only

e.g. a number
const constNum = 0; constNum = 1; assert.equal(constNum, 0);
or a string
const constString = 'I am a const'; constString = 'Cant change you?'; assert.equal(constString, 'I am a const');

complex types are NOT fully read-only

array`s items can be changed
const arr = []; arr[0] = 0; assert.equal(arr[0], 42);
object`s can be modified
const obj = {x: 1}; obj.x = 2; assert.equal(obj.x, 3);

Required Knowledge

Related Katas

Block scope

Difficulty Level

BEGINNER

First Published

24 March 2015

Stats

4 tests to solve