11. Built-in Functions in JS
String Built in Function
Method | Description | Examples |
charAt(index) | Returns the character at the specified index | "Hello".charAt(1) → "e" |
concat(str1, str2, ...) | Joins two or more strings | "Hello".concat(" World") → "Hello World" |
includes(substring) | Checks if a string contains another string | "JavaScript".includes("Java") → true |
indexOf(substring) | Returns the index of the first occurrence of a substring | "Hello World".indexOf("World") → 6 |
slice(start, end) | Extracts a section of a string | "JavaScript".slice(0, 4) → "Java" |
substring(start, end) | Similar to slice , but doesn’t accept negative indices | "JavaScript".substring(4, 10) → "Script" |
substr(start, length) | Extracts part of a string, specifying length | "JavaScript".substr(4, 6) → "Script" |
toLowerCase() | Converts to lowercase | "HELLO".toLowerCase() → "hello" |
toUpperCase() | Converts to uppercase | "hello".toUpperCase() → "HELLO" |
replace(search, replacement) | Replaces a substring with another string | "Hello".replace("H", "J") → "Jello" |
split(separator) | Splits a string into an array | "a,b,c".split(",") → ["a", "b", "c"] |
Array Built in Functions
push(element) | Adds an element at the end | arr.push(5) → adds 5 |
pop() | Removes the last element | arr.pop() → removes last item |
unshift(element) | Adds an element at the beginning | arr.unshift(1) → adds 1 |
shift() | Removes the first element | |
includes(element) | Checks if an element exists | [1, 2, 3].includes(2) → true |
slice(start, end) | Extracts part of an array | |
splice(start, count, item1, item2, ...) | Modifies an array | [1, 2, 3].splice(1, 1, 99) → [1, 99, 3] |
map(callback) | ransforms each element | [1, 2, 3].map(x => x * 2) → [2, 4, 6] |
filter(callback) | Filters elements | [1, 2, 3].filter(x => x > 1) → [2, 3] |
reduce(callback, initial) | Reduces array to a value | [1, 2, 3].reduce((a, b) => a + b, 0) → 6 |
find(callback) | Returns the first matching element | [1, 2, 3].find(x => x > 1) → 2 |
reverse() | reverse the array | [1, 2, 3].reverse() → [3, 2, 1] |
Maths Built-in functions
Math.abs(x) | Returns the absolute value | Math.abs(-10) → 10 |
Math.ceil(x) | Rounds up to the nearest integer | Math.ceil(4.2) → 5 |
Math.floor(x) | Rounds down to the nearest integer | Math.floor(4.9) → 4 |
Math.round(x) | Rounds to the nearest integer | |
Math.sqrt(x) | Returns the square root | Math.sqrt(25) → 5 |
Math.pow(x, y) | Returns x raised to power y | Math.pow(2, 3) → 8 |
Math.random() | Returns a random number between 0 and 1 | Math.random() → 0.245... |
Math.max(a, b, ...) | return max vakue | |
Math.min(a, b, ...) | return min value |
Date built-in functions
new Date() | Creates a new date object | new Date() → Current Date |
getFullYear() | Gets the full year | new Date().getFullYear() → 2025 |
getMonth() | Gets the month (0-11) | new Date().getMonth() → 1 |
getDate() | Gets the day of the month (1-31) | new Date().getDate() → 8 |
getDay() | Gets the day of the week (0-6) | new Date().getDay() → 6 |
getHours() | Gets the hours (0-23) | new Date().getHours() → 10 |
getMinutes() | Gets the minutes (0-59) | new Date().getMinutes() → 30 |
getSeconds() | Gets the seconds (0-59) | new Date().getSeconds() → 45 |
Interview Questions
let str = "Hello JavaScript!"; console.log(str.charAt(6)); console.log(str.indexOf("Java")); console.log(str.includes("Script"));
Output:
J
6
true
What does
Math.random()
return, and how can you generate a random number between 1 and 10?Math.random()
returns a random decimal between 0 (inclusive) and 1 (exclusive).let randomNum = Math.floor(Math.random() * 10) + 1; console.log(randomNum);
What is the difference between
map()
andforEach()
?map() → Transforms elements and creates a new array
foreach() → Iterates but does not return a new array
let arr = [1, 2, 3]; let newArr = arr.map(x => x * 2); console.log(newArr); // [2, 4, 6] arr.forEach(x => console.log(x * 2)); // Logs: 2, 4, 6
What will be the output ?
let numbers = [1, 2, 3, 4, 5]; let result = numbers.splice(2, 2, 99); console.log(numbers); console.log(result); /* output [1, 2, 99, 5] [3, 4]
splice(2, 2, 99)
removes 2 elements from index 2 ([3, 4]
) and replaces them with99
.How do you format a JavaScript
Date
object into a readable string?let date = new Date(); console.log(date.toDateString()); // Example: "Sat Feb 08 2025" console.log(date.toISOString()); // Example: "2025-02-08T10:30:00.000Z" console.log(date.toLocaleDateString()); // Example: "2/8/2025"
What will be the output ?
console.log(typeof NaN); // number console.log(isNaN("Hello")); // true // "Hello" is not a number, so isNaN("Hello") → true. console.log(Number(" 123 "));// 123 console.log(parseInt("0123", 10)); //123