JavaScript Fundamentals
JavaScript Data Types
JavaScript Data Types
Section titled “JavaScript Data Types”JavaScript has dynamic typing - variables can hold any type of value.
Primitive Types
Section titled “Primitive Types”Strings
Section titled “Strings”const single = 'Hello';const double = "World";const template = `Hello, ${name}!`;
// String methods"hello".toUpperCase(); // "HELLO""hello".length; // 5"hello".charAt(0); // "h""hello".includes("ell"); // trueNumbers
Section titled “Numbers”const integer = 42;const float = 3.14;const negative = -10;const infinity = Infinity;const notANumber = NaN;
// Number methodsNumber.isInteger(42); // trueNumber.parseFloat("3.14"); // 3.14(3.14159).toFixed(2); // "3.14"Booleans
Section titled “Booleans”const isTrue = true;const isFalse = false;
// Truthy values: true, 1, "string", [], {}// Falsy values: false, 0, "", null, undefined, NaNReference Types
Section titled “Reference Types”Arrays
Section titled “Arrays”const arr = [1, 2, 3, 4, 5];
arr.push(6); // Add to endarr.pop(); // Remove from endarr.map(x => x * 2); // [2, 4, 6, 8, 10]arr.filter(x => x > 2); // [3, 4, 5]arr.reduce((a, b) => a + b, 0); // 15Objects
Section titled “Objects”const person = { name: "Alice", age: 25, greet() { return `Hi, I'm ${this.name}`; }};
person.name; // "Alice"person["age"]; // 25Object.keys(person); // ["name", "age", "greet"]Type Checking
Section titled “Type Checking”typeof "hello" // "string"typeof 42 // "number"typeof true // "boolean"typeof undefined // "undefined"typeof null // "object" (quirk!)typeof [] // "object"typeof {} // "object"Array.isArray([]) // true