Lesson 2 of 18 · 0 of 2 exercises solved
Variables & Types
You will learn how to store values with let and const, what types JavaScript has, and why is safer than ==. These basics appear in every program you write.
Use const for values that don't change. Use let for values that do. Avoid var — it has confusing scoping rules and -level instead of block scope. There is no reason to use it in new code.
// const — cannot be reassigned
const PI = 3.14159;
const name = "Alice";
// let — can be reassigned
let age = 30;
age = 31; // fine
// JavaScript's primitive types
const integer = 42;
const float = 3.14; // both are 'number' type
const text = "hello";
const template = `Hello, ${name}`;
const flag = true;
const nothing = null; // intentional absence of value
const notDefined = undefined; // uninitialized
// Check types with typeof
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" ← famous bug, kept for compatibilityJavaScript is weakly typed, which means it will automatically convert types when comparing values. This is the source of many surprises. Use === (triple equals) for comparison — it checks type AND value.
// == does type coercion (avoid)
console.log(1 == "1"); // true ← surprising
console.log(0 == false); // true ← also surprising
// === checks type AND value (use this)
console.log(1 === "1"); // false ← correct
console.log(0 === false); // false ← correct
// Type conversion
const num = Number("42"); // 42
const str = String(42); // "42"
const bool = Boolean(0); // false
const bool2 = Boolean("hi"); // true
// Falsy values in JS: false, 0, "", null, undefined, NaN
// Everything else is truthy
if ("") console.log("won't print"); // empty string is falsy
if ("a") console.log("will print"); // non-empty string is truthyCommon question
Are var, let and const just three names for the same thing?
A declaration creates a name with a scope and a rule about reassignment. const is the default: the name is bound once, inside its block. let is for the few names that need to change, like counters. var is a leftover with function-wide scope and ; you only need it for code that must run on very old engines.
Alongside this is JavaScript's loose typing: operators convert values by rules that are consistent but surprising. Convert explicitly with Number, and , use === instead of ==, and let your declarations show what changes and what does not.
Source: ECMAScript 2015 added let and const because var's scoping rules had been a steady source of .
Common mix-ups
Each of these sounds reasonable. Think about why it is wrong, then open the explanation.
All three declare a , so which one I use is only a matter of taste.
const makes an object .
Since JavaScript converts types automatically, "5" + 3 and "5" - 3 behave the same way.
Practice
This program crashes because it reassigns a const. Fix the declaration so the counter can change, and make sure count ends at 3.
const versus let
const count = 0
count = count + 1
count += 2
console.log(count)
Press Run to try your code, then Check answer to grade it.The quantity arrives as a string from a form. Convert it to a number, compute total as quantity times price, and build label with a so it reads Total: 50.
Strings into numbers
const quantityText = "5"
const price = 10
const total = quantityText * price // works by accident — be explicit
const label = "Total: " + total
console.log(label)
Press Run to try your code, then Check answer to grade it.Check yourself
What does "5" + 3 evaluate to in JavaScript?
Tip: use the ← and → keys to move between lessons.