JavaScript provides built-in types and objects for working with strings, numbers, dates, and more. Mastering these is essential for real-world development.
Strings represent text and are enclosed in single quotes, double quotes, or backticks.
let single = 'Hello';
let double = "World";
let template = `Hello, ${single}!`; // Template literal with interpolation
let str = "JavaScript is awesome";
console.log(str.length); // 21
console.log(str.toUpperCase()); // "JAVASCRIPT IS AWESOME"
console.log(str.toLowerCase()); // "javascript is awesome"
console.log(str.indexOf("is")); // 11
console.log(str.slice(0, 10)); // "JavaScript"
console.log(str.includes("awesome")); // true
console.log(str.split(" ")); // ["JavaScript", "is", "awesome"]
console.log(str.replace("awesome", "fun")); // "JavaScript is fun"
Template literals (backticks) allow embedded expressions and multi-line strings:
let name = "Bob";
let age = 30;
let greeting = `Hi, I'm ${name} and I'm ${age} years old.`;
let multiLine = `
This is
a multi-line
string
`;
JavaScript has a single Number type for both integers and floating-point numbers.
let integer = 42;
let float = 3.14;
let negative = -10;
// Parsing strings to numbers
console.log(parseInt("42px")); // 42
console.log(parseFloat("3.14em")); // 3.14
// Formatting
let num = 123.456;
console.log(num.toFixed(2)); // "123.46"
console.log(num.toPrecision(4)); // "123.5"
// Special values
console.log(Infinity); // Infinity
console.log(NaN); // NaN (Not a Number)
console.log(isNaN("hello")); // true
The Date object handles dates and times:
let now = new Date();
let specific = new Date(2025, 0, 15); // Jan 15, 2025 (months are 0-indexed)
let fromString = new Date("2025-01-15");
console.log(now.getFullYear());
console.log(now.getMonth()); // 0—11
console.log(now.getDate());
console.log(now.getDay()); // 0 (Sunday) — 6
console.log(now.toLocaleDateString("en-US"));
console.log(now.toISOString());
The Temporal API is a modern replacement for the Date object, providing immutable date/time types:
// Temporal is available in modern environments
// const today = Temporal.Now.plainDateISO();
// const dueDate = today.add({ days: 7 });
console.log(Math.PI); // 3.141592653589793
console.log(Math.round(4.7)); // 5
console.log(Math.floor(4.7)); // 4
console.log(Math.ceil(4.3)); // 5
console.log(Math.random()); // Random number between 0 and 1
console.log(Math.max(1, 5, 3)); // 5
console.log(Math.min(1, 5, 3)); // 1
RegExp is used for pattern matching in strings:
let pattern = /hello/i; // Case-insensitive
let text = "Hello World";
console.log(pattern.test(text)); // true
console.log(text.match(pattern)); // ["Hello"]
console.log(text.replace(/world/i, "JS")); // "Hello JS"
// Implicit conversion
console.log("5" + 3); // "53" (string concatenation)
console.log("5" - 3); // 2 (numeric subtraction)
// Explicit conversion
console.log(Number("42")); // 42
console.log(String(42)); // "42"
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean("text")); // true
typeof to check the type: typeof 42 // "number". Be careful with implicit coercion; it often leads to bugs.
Math.random() and Math.floor(). Finally, use a template literal to display both results.