Learn about JavaScript variables (let, const, var), primitive data types, type coercion, operators, and template literals.
Variables, Types & Operators
Variables store data. Types describe what kind of data you have. Operators let you do things with that data. This chapter covers all three.
Declaring variables
JavaScript has three keywords for declaring variables: let, const, and var.
let - a variable that can change
Result:
const - a variable that cannot be reassigned
const does not mean the value is immutable - it means the binding cannot change. Objects and arrays declared with
const can still have their contents modified (more on this in later chapters).
var - the old way
Result:
var is function-scoped instead of block-scoped, which leads to confusing behavior. Always use let or const
instead. You will see var in older code, but there is no reason to use it in new code.
When to use let vs const
Use const by default. Switch to let only when you need to reassign the variable. This makes your code easier to
reason about - if you see const, you know the binding never changes.
Primitive data types
JavaScript has seven primitive types. A primitive is a value that is not an object and has no methods of its own ( though JavaScript auto-wraps them when you call methods).
String
Text data, wrapped in quotes:
Result:
All three quote styles produce the same string. Backticks (template literals) have extra powers - covered below.
Number
JavaScript has one number type for both integers and decimals:
Result:
Special number values:
Result:
The 0.1 + 0.2 result is a famous floating-point quirk, not a JavaScript bug. It happens in every language that uses
IEEE 754 floating-point numbers.
BigInt
For integers larger than Number.MAX_SAFE_INTEGER (2^53 - 1):
Result:
You will rarely need BigInt, but it exists for when you do.
Boolean
true or false:
Result:
null
Represents an intentional absence of value:
Result:
undefined
Represents a variable that has been declared but not assigned:
Result:
The difference: null means "explicitly nothing", undefined means "not yet set".
Symbol
A unique, immutable identifier. Used mainly for object property keys that should not collide:
Result:
Symbols are an advanced feature. You will not need them as a beginner, but knowing they exist is useful.
The typeof operator
typeof returns a string describing the type of a value:
Result:
Notice that typeof null returns "object". This is
a well-known historical bug
in JavaScript that was never fixed because too much code depends on it.
Template literals
Backtick strings support interpolation and multi-line text:
Result:
Use template literals whenever you need to embed values in strings. They are cleaner than string concatenation.
Type coercion
JavaScript automatically converts types when it thinks you need it to. This is called coercion and it is the source of many beginner mistakes.
String coercion
The + operator with a string converts the other value to a string:
Result:
The first 1 + "2" produces "12" (string), then "12" + 3 produces "123".
Number coercion
Other arithmetic operators convert strings to numbers:
Result:
Explicit conversion
Do not rely on coercion. Convert explicitly:
Result:
Falsy values
These values are false when converted to boolean:
Everything else is truthy - including "0", "false", [], and {}.
Result:
Arithmetic operators
Result:
Increment and decrement
Result:
Assignment operators
Shorthand for modifying a variable:
Result:
Comparison operators
Comparisons return a boolean (true or false):
Result:
Equality: === vs ==
Always use === (strict equality). It checks both value and type:
Result:
== (loose equality) performs type coercion before comparing, which leads
to surprising results. Avoid it.
Similarly, use !== (strict inequality) instead of !=:
Result:
Logical operators
Result:
Short-circuit evaluation
&& and || do not always return booleans - they return the value that determined the result:
Result:
This pattern is commonly used for default values:
Result:
Nullish coalescing (??)
|| treats 0, "", and false as falsy. If you only want to fall back on null or undefined, use ??:
Result:
String operations
Result:
Summary
- Use
constby default,letwhen you need to reassign. Avoidvar. - JavaScript has seven primitive types: string, number, bigint, boolean, null, undefined, symbol.
typeofreveals a value's type (with thenullquirk).- Template literals (backticks) support interpolation and multi-line strings.
- Type coercion is implicit and often surprising - convert explicitly with
Number(),String(),Boolean(). - Always use
===and!==for comparisons. ??is safer than||for default values when0,"", orfalseare valid.
Next up: Control Flow - making decisions and repeating actions.