JavaScript Data Structures
Learn about JavaScript's data types and data structures.
1. Dynamic Typing
JavaScript is a dynamically typed language. This means you don't have to specify the data type of a variable when you declare it.
JavaScript Code
Live JavaScript representation with highlighted changes
let foo = 42; // foo is now a numberfoo = "bar"; // foo is now a stringfoo = true; // foo is now a booleanconsole.log(typeof foo);
2. Type Coercion
JavaScript is also weakly typed, which means it allows implicit type conversion when an operation involves mismatched types.
JavaScript Code
Live JavaScript representation with highlighted changes
const foo = 42; // foo is a numberconst result = foo + "1"; // JavaScript coerces foo to a stringconsole.log(result);
3. Primitive Types
JavaScript has seven primitive data types. Let's check their types with the typeof
operator.
JavaScript Code
Live JavaScript representation with highlighted changes
console.log(typeof 42); // numberconsole.log(typeof "hello"); // stringconsole.log(typeof true); // booleanconsole.log(typeof 123n); // bigintconsole.log(typeof Symbol('id')); // symbolconsole.log(typeof undefined); // undefinedconsole.log(typeof null); // object (this is a famous quirk!)
Console Output