List of immutable functions in JavaScript
-
1.0 slice
// extracts certain section of a string and return a new string // doesn't mutate the original string var name = 'Stephanie Hwang' var first = name.slice(0, 8) console.log(name) //Stephanie Hwang console.log(first) //Stepanie console.log(name) //Stephanie Hwang
-
2.0 concat
// concatenate two strings // returns a new string var first = 'Stephanie' var last = ' Hwang' var fullName = first.concat(last); console.log(first) //Stephanie console.log(last) //Hwang console.log(fullName) //Stephanie Hwang console.log(first) //Stephanie console.log(last) //Hwang
-
3.0 replace
// replaces matching section of a the string which is explicitly passed to replace function var re = /Harry/gi var str = "Harry is a memeber of gryffindor." var newstr = str.replace(re, "Hermione") console.log(str) //Harry is a memeber of gryffindor. console.log(newstr) //Hermione is a memeber of gryffindor. console.log(str) //Harry is a memeber of gryffindor.
-
4.0 split
//split a given string into an array of strings var str = "Apples are round, and apples are juicy."; var newstr = str.split(" ", 2); console.log(str) //Apples are round, and apples are juicy. console.log(newstr) //['Apples', 'are'] console.log(str) //Apples are round, and apples are juicy.
-
5.0 substr
//extracts part of a string var str = "Ten points for gryffindor" var newstr = str.substr(0, 3) console.log(str) //Ten points for gryffindor console.log(newstr) //Ten console.log(str) //Ten points for gryffindor
-
6.0 substring
//extracts part of a string var str = "Ten points for gryffindor" var newstr = str.substring(0, 3) console.log(str) //Ten points for gryffindor console.log(newstr) //Ten console.log(str) //Ten points for gryffindor
-
7.0 toLocaleLowerCase
//converts string to lowercase by keeping it's current locale var str = "Ron loves Hermionie" var newstr = str.toLocaleLowerCase() console.log(str) //Ron loves Hermionie console.log(newstr) //ron loves hermionie console.log(str) //Ron loves Hermionie
-
8.0 toLocaleUpperCase
//converts string to uppercase by keeping it's current locale var str = "Ron loves Hermionie" var newstr = str.toLocaleUpperCase() console.log(str) //Ron loves Hermionie console.log(newstr) //RON LOVES HERMIONIE console.log(str) //Ron loves Hermionie
-
9.0 toString
//converts type to represent string var phone = 124587921 var phonestr = phone.toString() console.log(phone) //124587921 console.log(phonestr) // "124587921" console.log(phone) //124587921
-
10.0 toLowerCase
//converts string to lowercase var str = "Ron loves Hermionie" var newstr = str.toLocaleLowerCase() console.log(str) //Ron loves Hermionie console.log(newstr) //ron loves hermionie console.log(str) //Ron loves Hermionie
-
11.0 toUpperCase
//converts string to uppercase var str = "Ron loves Hermionie" var newstr = str.toLocaleUpperCase() console.log(str) //Ron loves Hermionie console.log(newstr) //RON LOVES HERMIONIE console.log(str) //Ron loves Hermionie
-
12.0 map
//iterates through an array var names = ['rajika', 'stephanie', 'taeyeon'] names.map(function(name) { console.log(name) }) //es6 syntax, arrow function //names.map((name) => { // console.log(name) //}) console.log(names)
-
13.0 reduce
var arr = [1,2,3,4] var total = arr.reduce(function(prev, curr, index, arr) { return prev + curr }); console.log(arr) // [1,2,3,4] console.log(total) //10 console.log(arr) // [1,2,3,4] //es6 syntax, arrow function //var total = arr.reduce((prev, curr, index, arr) => { //return prev + curr //});