Javascript Ten Tips

Azizul Haque
4 min readNov 2, 2020

01| Includes Methods of String and Array

Image01: String includes method
Image02: Array includes method
  • Includes method give Boolean value that is true and false.
  • When we called the includes method (Ex:Image: 01, Line.No: 4&5) it checks the const sentence(Ex:Image: 01, Line.No: 3) for matching.
  • If matches then give true value. or not matches give false value(Ex: Image: 01, Line.No: 4&5).
  • A similar thing happened in the array. It gives true and false value and that’s example is an image no 02.

02| String concatMethod

Image: 03
  • This method works concatenation one object to another object
  • In this image line 4 and 5 is declared variable and line 6 is another variable that concatenation firstName and lastName variable
  • The output is line 7

03|substr Methods:

Image: 04
string.substr(start , [length(Optional)])//start is index number
  • You started the first character this is number one.
  • See the second value length and take the character. (optional)
  • If you don't use the second value it takes all character from the first value.
  • Image 04 we started index no eight-character and take four-character after eight-character.
  • So The output is subs.

04| isNaN Testing:

Image: 05
  • Only isNaN(NaN) and isNaN(undefined) return true value.
  • isNaN(true) and is NaN(false) return false value.
  • In this image, you can see the difference.
  • Line No: 76 that returns true value so it returns Number.
  • Line No:77 that returns a false value. If you give a string, isNaN is trying to convert string to a number.
  • If it converts Number it gives false value

05| Math.abs:

const number1 = 10.3;
const number2 = 4.8;
const substruction = number2 - number1;
console.log(Math.abs(substruction))
//output is 5.50000

Math.abs Methods

  • It gives absolute value.
  • Never return a negative value.
  • If you give a negative value it returns a positive value always.

06 | Math.cail, Math.floor:

const number1 = 10.3;
const number2 = 4.8;
const total = number1 + number2;
console.log(Math.ceil(total));
//orginal output is 15.1.
console.log(Math.floor(total));
//orginal output is 15.1.

Math.ceil Methods

  • It returns all time ceil value.
  • When you use Math.ceil() the original value is “15.1”. But for using the ceil method the value is “16”.

Math.floor Methods

  • It returns all-time floor value.
  • When you use Math.ceil() the original value is “15.1”. But for using the ceil method the value is “16”.

07 | Math.sqrt:

function total(x, y) {
return (Math.sqrt(x*y));
}
const newTotal = total(12,12);
console.log(newTotal);
  • This method contains a single parameter.
  • If the parameter is negative then the value is “NaN”
  • The parameter which holds the number whose square root is to be calculated.
  • The value is 12.

Math.random()

Math.random(1)
//value up and down 0 to 1;
  • This is a method where the value up to down in 0 to 1 randomly;

08| every Method:

const number1 = [ 11, 12, 32, 41, 53 ];
const number2 = [ -3, -1, -3];
function total(number){
return number <0;
}
console.log(number1.every(total));// returns false
console.log(number2.every(total)); // returns true
  • This method returns a Boolean value.
  • If the condition is tracked and matched then returns true. Not matches return a false value.
  • every method checks all elements in the array. If one element is unmatched return a false value.

09| Spread Operator:

const number1 = [1,2,3,4];
const number2 = [6,7,8,9];
const totalNumber = [...number1, ...number2]
console.log(totalNumber)
//output is [1,2,3,4,6,7,8,9]
  • This is similar to concatmethod.
  • But using triple-dot that is coming ECMAScript 6 Model
  • It made easier programmer life.
  • You can easily concatenation one array to another array or any value.
  • It also works in Object.

10| For of Loop and For in Loop:

const arr = ["I", "Love", "Programming"];
for (let element of arr) {
console.log(element); //output is I, Love, Programming;
}

For of Loop:

  • For of Loop using for Array.
  • It is the new features in ECMAScript 6 model.
const singleProduct = {
productName: "Mango",
productPrice: 100,
productQuantity: 10,
};
for (let element in singleProduct) {
console.log(element); // output id productName, productPrice, productQuantity;
console.log(singleProduct[element]); // output is Mango, 100 , 10;
}

For in Loop:

  • For in Loop using for Object.
  • It is the new features in ECMAScript 6 model.

11| Rest Operator:

const arr1 = ["I", "Love", "Programming"];
const [index1, ...restArray] = arr1;
console.log(index1, restArray); //output is ["I" ["Love", "Programming"]];
  • It is the rest operator that looks to spread the operator. But work differently.
  • It changes the array, takes a first value (Ex. index1), second value is (Ex. …restArray).
  • If you want another separate like as index1 you can take a separate value.
  • Then it shows other value in the …restArray.

--

--