Methods Flashcards
Array.prototype.shift()
The shift() method removes the first element from an array and returns that element. This method changes the length of the array.
var a = [1, 2, 3]; var b = a.shift();
console. log(a); // [2, 3]
console. log(b); // 1
arr.shift()
Array.prototype.slice()
The slice() method returns a new array object selected from begin to end (end not included). The original array will not be modified.
arr. slice()
arr. slice(begin)
arr. slice(begin, end)
var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']; var citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'] // citrus contains ['Orange','Lemon']
Array.prototype.splice()
The splice() method changes the contents of an array by removing existing elements and/or adding new elements. Return value: An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
Syntax
array. splice(start)
array. splice(start, deleteCount)
array. splice(start, deleteCount, additem1, additem2, …)
if deleteCount is 0 then no elements are removed from the array
var myFish = [‘angel’, ‘clown’, ‘mandarin’, ‘sturgeon’];
myFish.splice(2, 0, 'drum'); // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
myFish.splice(2, 1); // myFish is ["angel", "clown", "sturgeon"]
for…in
The for…in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed
Object.prototype.constructor
Returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function’s name. The value is only read-only for primitive values such as 1, true and “test”.
Math.floor()
The Math.floor() function returns the largest integer less than or equal to a given number.
Math.floor( 45.95); // 45 Math.floor( 45.05); // 45 Math.floor( 4 ); // 4 Math.floor(-45.05); // -46 Math.floor(-45.95); // -46
Math.pow()
The Math.pow() function returns the base to the exponent power, that is, base**exponent.
Syntax
Math.pow(base, exponent)
Array.prototype.map()
The map() method creates a new array with the results of calling a provided function on every element in this array. Return value: A new array with each element being the result of the callback function.
var numbers = [1, 4, 9]; var roots = numbers.map(Math.sqrt); // roots is now [1, 2, 3] // numbers is still [1, 4, 9]
Number.prototype.toString()
The toString() method returns a string representing the specified Number object.
console. log(count.toString()); // displays ‘10’
console. log((17).toString()); // displays ‘17’
console. log((17.2).toString()); // displays ‘17.2’
toUpperCase()
The toUpperCase() method returns the value of the string converted to upper case. toUpperCase() does not affect the value of the string itself.
Return value
A new string representing the calling string converted to upper case.
console.log(‘alphabet’.toUpperCase()); // ‘ALPHABET’
Math.random()
The Math.random() function returns a random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.
Array.prototype.every()
The every() method tests whether all elements in the array pass the test implemented by the provided function.
function isBigEnough(element, index, array) { return element >= 10; }
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
Syntax
arr.every(callback[, thisArg])
Array.prototype.some()
The some() method tests whether some element in the array passes the test implemented by the provided function.
function isBiggerThan10(element, index, array) { return element > 10; }
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
Syntax
arr.some(callback[, thisArg])
String.prototype.indexOf()
The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
charCodeAt()
var str = “HELLO WORLD”;
var n = str.charCodeAt(0);
The result of n will be:
72
to use this properly:
.toLowerCase() on the string
str.charCodeAt(x) - 96;