Skip to content

Useful javascript functions

toString()

strings

parseInt(str), parseFloat(str)
str.split(‘’), //returns array

arrays

arr.sort(), // returns array

[2, 1, 2, 30, 12, 4,].sort(); // [1, 12, 2, 2, 30, 4]
[2, 1, 2, 30, 12, 4,].reverse(); // [1, 12, 2, 2, 30, 4]

arr.reverse(), //returns array

[2, 1, 2, 30, 12, 4].reverse(); // [1, 12, 2, 2, 30, 4]

arr.join(), // returns string

['Fire', 'Wind', 'Rain'].join('-') //Fire-Wind-Rain

arr.map(), //returns array

arr.reduce() // returns result

// sum
[0, 1, 2, 3, 4].reduce( (prev, curr) => prev + curr ); // 6
// flattened 
[[0, 1], [2, 3], [4, 5]].reduce((a, b) => a.concat(b),[]); // [0,1,2,3,4,5]

regex

RegexPattern.test(str) // true or false
‘abc’.match(/a/); // [“a”, index: 0, input: “abc”]

Published inJS

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *