Some Important Javascript Method
Some important JavaScript are -
1) String.slice():
It copies a section of a string and return it as a new string without modifying the original string.
Syntax:
string.slice(start,end)
Example:
const str = “Hello Bangladesh”;
console.log(str.slice(0,6));
//output:Hello
2) Global isNaN() Vs Number.isNaN():
isNaN is function that checks whether given value is a valid number or not. If the value is invalid number (Not-a-number) then it returns true otherwise it returns false.
Here, the global function isNaN(), first converts the tested value to a number and then evaluate it but Number.isNaN() directly tests it without any conversion.
Example:
console.log(isNaN(new Date().toString()));
//string converted to number and return true
console.log(Number.isNaN(new Date().toString()));
//return false
3) Array.splice():
This method changes the existing array by removing or replacing the array.
Syntax:
Array.splice( index, remove_count, item_list )
Example:
let languages = [‘C++’, ‘Java’, ‘Html’, ‘Python’, ‘C’];
// Add ‘javascript’ and ‘Php’ after removing ‘Html’.
let removed = languages.splice(2, 1, ‘javascript’, ‘Php’)
4) Array.join():
This method creates a string by concatenating the array element using an optional separator parameter.
Example:
const arr = [“amar”,”sonar”,”bangla”];
console.log(arr.join(“-”));
//output: amar-sonar-bangla
5) Array.shift() and unshift():
Shift() method deletes the first element of an array. Unshift() method add a new element at first index of an array.
Example:
const arr = [“amar”,”sonar”,”bangla”];
const del = arr.shift();
console.log(arr);
//output:[ ‘sonar’, ‘bangla’ ]
const add = arr.unshift(“bangladesh”);
console.log(arr);
//output: [ ‘bangladesh’, ‘sonar’, ‘bangla’ ]
6) Object.assign():
This method is used to copy one or more source objects to a target object. It takes source objects and a target object as parameters and push the source objects into the target object and display the target object.
Example:
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
//output: { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
//output: { a: 1, b: 4, c: 5 }
Here is a warning for deep cloning, If the source value is a reference to an object, it only copies the reference value.
Example:
let obj1 = { a: 0 , b: { c: 0}};
let obj2 = Object.assign({}, obj1);
console.log(JSON.stringify(obj2)); // { “a”: 0, “b”: { “c”: 0}}
obj2.b.c = 3;
console.log(JSON.stringify(obj1)); // { “a”: 1, “b”: { “c”: 3}}
console.log(JSON.stringify(obj2)); // { “a”: 2, “b”: { “c”: 3}}
7) Object.create():
This method creates a new object, with the specified prototype. Newly created object can access all the properties of prototype object. It can take an optional parameter to add new properties.
Example:
const fruits = {test:’sour’}
const mango = Object.create(fruits, {
name:{value:’Rajshahi mango’}
});
console.log(‘name: ‘+mango.name + ‘, test: ‘+ mango.type)
//output: name: Rajshahi mango, test: sour
8) Object.values() and Object.keys():
These methods return an array with values and keys of the object.
Example:
const object1 = {
a: ‘somestring’,
b: 42,
c: false
};
console.log(Object.values(object1)); // output:[“somestring”, 42, false]
console.log(Object.keys(object1)); //output: [“a”, “b”, “c”]
9) Object.freeze()
This method freezes an object. A frozen object can no longer be changed;
Example:
const obj = { prop: 42 };
Object.freeze(obj);
obj.prop = 33;
// Throws an error in strict mode
console.log(obj.prop);
// expected output: 42
10) Math.round():
It returns the value of a number rounded to the nearest integer.
Example:
console.log(Math.round(0.9));
//output: 1