DEV Community

Liz P
Liz P

Posted on

Some of JavaScript's Built-In Methods

Is JavaScript confusing? It can be, sure. But it’s pretty amazing, especially with it’s built-in methods that can reduce the amount of code written and help with readability. Here a few JS built-in methods that you’re probably already using or have at least seen more than once.

Concat()- this method will join two (or more) strings together and return a new string.

let str = "Hey there! "
let str2 = "JavaScript is fun."

console.log(str.concat(str2)) // "Hey there! JavaScript is fun.”
Enter fullscreen mode Exit fullscreen mode

Length()- this method will return the length of the given string.

let str = "Hey there!"
let str2 = "JavaScript is fun."

console.log(str.length) // 10
console.log(str2.length) // 18
Enter fullscreen mode Exit fullscreen mode

ToUpperCase()- converts the given string to uppercase.

let str = "Hey there! JavaScript is fun."

console.log(str.toUpperCase()) // "HEY THERE! JAVASCRIPT IS FUN."
Enter fullscreen mode Exit fullscreen mode

ToLowerCase()- converts the given string to lowercase.

let str = "Hey there! JavaScript is fun."

console.log(str.toLowerCase()) // "hey there! javascript is fun."
Enter fullscreen mode Exit fullscreen mode

Split()- splits the string into substrings which are placed in an array and using a separator you choose.

let str = "Hey there! JavaScript is fun."

console.log(str.split('')) // ["H", "e", "y", " ", "t", "h", "e", "r", "e", "!", " ", "J", "a", "v", "a", "S", "c", "r", "i", "p", "t", " ", "i", "s", " ", "f", "u", "n", "."]
// here the string is spilt into characters, using no space as a separator

console.log(str.split(' ')) // ["Hey", "there!", "JavaScript", "is", "fun."] 
// here the string is split into separate strings using a space as the separator
Enter fullscreen mode Exit fullscreen mode

Join()- joins an arrays elements and returns a new string, as with split you can choose your separator.

let greetings = ["Hi", "Hello", "Hey"]

console.log(greetings.join('')) // “HiHelloHey"
// here the elements are joined with no space as the separator

console.log(greetings.join(' ')) // " Hi Hello Hey”
// here the elements are joined with a space as the separator
Enter fullscreen mode Exit fullscreen mode

Reverse()- reverses the order of the given array’s elements.

let animals = ["Lions", "Tigers", "Bears"]

console.log(animals.reverse()) // ["Bears", "Tigers", “Lions"]
Enter fullscreen mode Exit fullscreen mode

Pop()- removes the last element of the given array and returns it.

let animals = ["Lions", "Tigers", "Bears"]

console.log(animals.pop()) // "Bears"
Enter fullscreen mode Exit fullscreen mode

Push()- adds one or more elements to the end of the given array and returns the new length of that array. (If you log the array to the console again, you’ll see your element(s) are now added)

let animals = ["Lions", "Tigers", "Bears"]

console.log(animals.push("Monkies")) // 4
console.log(animals) // ["Lions", "Tigers", "Bears", "Monkies"]
Enter fullscreen mode Exit fullscreen mode

Shift()- removes the first element of the given array and returns it.

let animals = ["Lions", "Tigers", "Bears"]

console.log(animals.shift()) // "Lions"
Enter fullscreen mode Exit fullscreen mode

Unshift()- adds one or more elements to the beginning of the given array and returns the new length of that array. (If you log the array to the console again, you’ll see your element(s) are now added)

let animals = ["Lions", "Tigers", "Bears"]

console.log(animals.unshift("Monkies")) // 4
console.log(animals) // ["Monkies", "Lions", "Tigers", “Bears"]
Enter fullscreen mode Exit fullscreen mode

These are just a few of the built-in methods JavaScript gives us for free. If you want to see more I suggest checking out MDN for a more comprehensive list. Thanks for reading!

Top comments (0)