Declaring variables
- let: block-scoped, reassignable
- const: block-scoped, not reassignable (but objects/arrays are still mutable)
let count = 0;
count = count + 1; // ok
const siteName = 'Intro Blog';
// siteName = 'Other' // ❌ TypeError: Assignment to constant variable.
Common types
- number, string, boolean, null, undefined, object, symbol, bigint
const age = 21; // number
const name = 'Ahbab'; // string
const isStudent = true; // boolean
const nothing = null; // null
let notSet; // undefined
const user = { id: 1, name } // object
const tags = ['html', 'css']; // array (also an object)
console.log(typeof age); // 'number'
console.log(typeof user); // 'object'
Practice
- Declare a
constfor your name and aletfor a counter; increment it. - Create an object describing a course with
title,hours, andtags. - Log the
typeofdifferent variables.
Next up
Quick Check
Which keyword declares a block-scoped constant?