Course Content
Data types and Values
0/1
Object-oriented programming in JavaScript
0/1
Error handling and debugging in JavaScript
0/1
JavaScript functions for string and array manipulation
0/1
JavaScript Libraries and Frameworks
0/1
JavaScript
About Lesson

Built-in Type Methods

Built-in type methods are predefined functions associated with various data types like strings, numbers, arrays, objects, and more. These methods provide a way to perform specific operations or manipulations on values of those data types. Here’s an overview of some common built-in type methods in JavaScript:

String Methods:

# Built-in Method Description Example
1 String.length Returns the length of a string. "Hello".length returns 5
2 String.toUpperCase() Converts a string to uppercase. "hello".toUpperCase() returns “HELLO”
3 String.toLowerCase() Converts a string to lowercase. "Hello".toLowerCase() returns “hello”
4 String.indexOf() Returns the index of a substring within a string. "apple".indexOf("pl") returns 2
5 String.substring() Returns a substring based on specified indices. "apple".substring(1, 4) returns “ppl”
6 String.split() Splits a string into an array of substrings based on a delimiter. "apple,banana,orange".split(",") returns [“apple”, “banana”, “orange”]
7 String.trim() Removes whitespace from both ends of a string. " hello ".trim() returns “hello”

Number Methods:

# Built-in Method Description Example
1 Number.toFixed() Formats a number to a fixed number of decimal places. (5.12345).toFixed(2) returns “5.12”
2 Number.toPrecision() Formats a number with a specified length. (123.456).toPrecision(4) returns “123.5”
3 Number.toString() Converts a number to a string. (123).toString() returns “123”
4 parseInt() Parses a string and returns an integer. parseInt("123") returns 123
5 parseFloat() Parses a string and returns a floating-point number. parseFloat("3.14") returns 3.14

Array Methods:

# Built-in Method Description Example
1 Array.push() Adds elements to the end of an array. let arr = [1, 2]; arr.push(3); modifies arr to [1, 2, 3]
2 Array.pop() Removes and returns the last element of an array. let arr = [1, 2, 3]; let last = arr.pop(); modifies arr to [1, 2], last contains 3
3 Array.shift() Removes and returns the first element of an array. let arr = [1, 2, 3]; let first = arr.shift(); modifies arr to [2, 3], first contains 1
4 Array.unshift() Adds elements to the beginning of an array. let arr = [2, 3]; arr.unshift(1); modifies arr to [1, 2, 3]
5 Array.concat() Combines arrays and returns a new array. let arr1 = [1, 2]; let arr2 = [3, 4]; let newArr = arr1.concat(arr2); newArr is [1, 2, 3, 4]
6 Array.join() Joins array elements into a string using a delimiter. let arr = ["apple", "banana", "orange"]; let result = arr.join(", "); result is “apple, banana, orange”
7 Array.slice() Extracts a section of an array and returns a new array. let arr = [1, 2, 3, 4, 5]; let subArr = arr.slice(1, 4); subArr is [2, 3, 4]
8 Array.splice() Adds or removes elements from an array at a specified index. let arr = [1, 2, 3, 4, 5]; arr.splice(2, 2, 6, 7); (Removes 2 elements starting from index 2, then add 6 and 7). Hence we get arr = [1, 2, 6, 7, 5]
9 Array.forEach() Executes a function for each array element. let arr = [1, 2, 3]; arr.forEach(item => console.log(item)); prints each element
10 Array.map() Creates a new array with the results of calling a function on each element. let arr = [1, 2, 3]; let doubled = arr.map(item => item * 2); doubled is [2, 4, 6]
11 Array.filter() Creates a new array with elements that pass a certain condition. let arr = [1, 2, 3, 4, 5]; let even = arr.filter(item => item % 2 === 0); even is [2, 4]
12 Array.reduce() Applies a function against an accumulator and each element to reduce the array to a single value. let arr = [1, 2, 3]; let sum = arr.reduce((accumulator, current) => accumulator + current, 0); sum is 6

Object Methods:

# Built-in Method Description Example
1 Object.hasOwnProperty() Checks if an object has a specific property. let person = { name: "Alice", age: 30 }; person.hasOwnProperty("name"); returns true
2 Object.keys() Returns an array of an object’s own enumerable property names. let person = { name: "Alice", age: 30 }; let keys = Object.keys(person); keys is [“name”, “age”]
3 Object.values() Returns an array of an object’s own enumerable property values. let person = { name: "Alice", age: 30 }; let values = Object.values(person); values is [“Alice”, 30]
4 Object.entries() Returns an array of an object’s own enumerable property key-value pairs. let person = { name: "Alice", age: 30 }; let entries = Object.entries(person); entries is [[“name”, “Alice”], [“age”, 30]]
5 Object.assign() Copies the values of all enumerable properties from one or more source objects to a target object. let target = {}; let source = { name: "Alice", age: 30 }; Object.assign(target, source); target is { name: “Alice”, age: 30 }
6 Object.toString() Returns a string representation of an object. let person = { name: "Alice", age: 30 }; let str = Object.toString(person); str is “[object Object]”

These are just a few examples of built-in type methods in JavaScript. JavaScript’s standard library provides a rich set of methods for manipulating various data types, making it easier to work with strings, numbers, arrays, objects, and other built-in types.