(ง •̀_•́)ง Ming's blog

Manipulating in JavaScript

References

Content

String and Array

concat

//string
var str1 = "Hello";
var str2 = "World";
console.log(str1.concat(" ", str2)); // expected output: "Hello World"
console.log(str2.concat(", ", str1)); // expected output: "World, Hello"
//array
var array1 = ["a", "b", "c"];
var array2 = ["d", "e", "f"];
console.log(array1.concat(array2)); // expected output: Array ["a", "b", "c", "d", "e", "f"]

slice

//string
var str = "The quick brown fox jumps over the lazy dog.";
console.log(str.slice(31)); // expected output: "the lazy dog."
console.log(str.slice(4, 19)); // expected output: "quick brown fox"
console.log(str.slice(-4)); // expected output: "dog."
console.log(str.slice(-9, -5)); // expected output: "lazy"
//array
var animals = ["ant", "bison", "camel", "duck", "elephant"];
console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5)); // expected output: Array ["bison", "camel", "duck", "elephant"]

String

replace

var p = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?";
var regex = /dog/gi;
console.log(p.replace(regex, "ferret"));
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
console.log(p.replace("dog", "monkey"));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"

trim

var greeting = "   Hello world!   ";
console.log(greeting); // expected output: "   Hello world!   ";
console.log(greeting.trim()); // expected output: "Hello world!";

charAt

var sentence = "The quick brown fox jumps over the lazy dog.";
var index = 4;
console.log("The character at index " + index + " is " + sentence.charAt(index));
// expected output: "The character at index 4 is q"

charCodeAt

var sentence = "The quick brown fox jumps over the lazy dog.";
var index = 4;
console.log(
 "The character code " + sentence.charCodeAt(index) + " is equal to " + sentence.charAt(index)
);
// expected output: "The character code 113 is equal to q"

fromCharCode

console.log(String.fromCharCode(189, 43, 190, 61));
// expected output: "½+¾="

Array

sort

var months = ["March", "Jan", "Feb", "Dec"];
months.sort();
console.log(months); // expected output: Array ["Dec", "Feb", "Jan", "March"]

var array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1); // expected output: Array [1, 100000, 21, 30, 4]

splice

var months = ["Jan", "March", "April", "June"];
months.splice(1, 0, "Feb"); // inserts at index 1
console.log(months); // expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
months.splice(4, 1, "May"); // replaces 1 element at index 4
console.log(months); // expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']

push

var animals = ["pigs", "goats", "sheep"];
console.log(animals.push("cows")); // expected output: 4
console.log(animals); // expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push("chickens");
console.log(animals); // expected output: Array ["pigs", "goats", "sheep", "cows", "chickens"]

pop

var plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"];
console.log(plants.pop()); // expected output: "tomato"
console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage"]

shift

var array1 = [1, 2, 3];
var firstElement = array1.shift();
console.log(array1); // expected output: Array [2, 3]
console.log(firstElement); // expected output: 1

unshift

var array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // expected output: 5
console.log(array1); // expected output: Array [4, 5, 1, 2, 3]