Step: Basics | Player: 01_JS_Getting Started
Learning Objectives
- Explains the differences between
let,const, andvarand allows you to choose correctly. - You can distinguish between the seven primitive types of JavaScript.
- The result of explicit/implicit type conversion can be predicted.
- You can distinguish between truthy and false values and use them in conditional expressions.
- You can check the type of value with the
typeofoperator.
1. Variable declaration: const, let, var
const cannot be reassigned, let can be reassigned, and var is not used in modern code due to function scope and hoisting issues. The default is const, and let is used only when the value changes.
const PI = 3.14;
let count = 0;
count += 1;
2. Primitive types
There are seven primitive types in JavaScript: string, number, boolean, null, undefined, symbol, and bigint. Compare/copy. as a value itself, as opposed to an object
typeof "hi"; // "string"
typeof 42; // "number"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; //"object" ← historical bug
3. Type conversion (Type coercion)
Explicit conversion is performed automatically by operators such as Number(), String(), and Boolean(), and implicit conversion is performed automatically by operators such as + and ==. Explicit conversion is recommended because unintentional conversion can cause bugs.
Number("42"); // 42
String(42); // "42"
"5" + 1; //"51" (string concatenation)
"5" - 1; //4 (number conversion)
4. truthy / falsy
There are only 7 values that evaluate to falsy in the conditional expression: false, 0, -0, 0n, "", null, undefined, and NaN. Everything else is truthy.
if ("") console.log("Not running");
if ("0") console.log("Executed - non-empty string");
Core example
| file | Contents covered |
|---|---|
01_let_const.js |
Reassign/Scope difference between const and let |
02_primitives.js |
7 primitive types and typeof |
03_conversion.js |
explicit/implicit type conversion comparison |
04_truthy_falsy.js |
truthy/falsy behavior in conditional expressions |
⚠️ Common Mistakes
- A misunderstanding that the properties of an object declared as
constcannot be changed (reassignment is not possible, but internal changes are possible). - Incorrectly checking for null without knowing it was
typeof null === "object". - I know that
parseInt("10px")is10, but I am confused thatNumber("10px")isNaN. - Traps of
==, such as"" == falseand0 == false. (Use===if possible) - When
letis sufficient, butvaris used to create a hoisting bug.
❓ Frequently Asked Questions (FAQ)
Q1. Do I always have to use const first? A. Yes. Change to let only when reallocation is necessary. Intention becomes clear.
Q2. What is the difference between null and undefined? A. undefined indicates "no value has been assigned", null indicates "intentionally empty".
Q3. When is BigInt used? A. Used when an integer exceeding Number.MAX_SAFE_INTEGER (2^53 - 1) is required. It is unnecessary for general arithmetic.