10 JavaScript concept you should read again

Sakib Noman
3 min readMay 5, 2021

1. Number type data

JavaScripts number data type is so different from integer of other programming languages. In C or Java, if we divide 5 by 2, we will found 2, but in JavaScript, we will found 2.5.

C#include<stdio.h>
int main()
{
printf(5/2)
}
//Output: 2JavaScriptconsole.log(5/2)//Output: 2.5

2. Convert string to number

We can convert string to number by using the parseInt() method. The notable point is that the parseInt() method can take two arguments. One argument is a string, and another argument is number base—for example, binary 2, octal 8, hexadecimal 16, decimal 10.

console.log(parseInt(“777”,16))  //output: 1911console.log(parseInt(“777”,8))   //output: 511console.log(parseInt(“777”,10))  //output: 777

3. Let vs Var

We can declare a variable by using let or var. But there is a difference between them. let has block-level scope. If we declare a variable by let it will make available only that block, wheres var has availability in the whole function scope.

if(true){
let x = 5;
console.log(x); //output: 5
}
console.log(x); //output: ReferenceError: x is not defined
if(true){
var x = 5;
console.log(x); //output: 5
}
console.log(x); //output: 5

4. Add String and Number

When we add a number with a string, it will be converted to a string. But there are two cases you should focus on.

‘4’+4+4 and 4+4+’4’ is totally different though we are adding number and string.

Case 1: In the first case first 4 and second 4 will make a string ‘44’, and the third four also make a string ‘444’.

Case 2: In the second case first two-four will make a sum which is 8, and the third four will make a string which is ‘84.’

console.log(4+4+’4') //output: ‘84’console.log(‘4’+4+4) //output: ‘444’

5. For of

Special javascript for loop, which makes loop operation easy.

Each element will be printed:

let myArray = [2,3,4,5];
for(let each of myArray){
console.log(each)
}
Output:
2
3
4
5

6. For in

This is another special for loop

The index will be printed in string format:

let myArray = [2,3,4,5,3];
for(let each in myArray){
console.log(each)
}
Output:
‘0’
‘1’
‘2’
‘3’
‘4’

7. charAt() method

We can easily access any character of a string by this method. We have to provide the position number.

let myString = “Hello World.”
console.log(myString.charAt(0))
//Output: ‘H’

8. trim() method

Whitespace in both ends of a string may create bugs. trim() is a useful method to remove whitespace in both ends.

let myStr = “ Removing White Space Both Ends “;
console.log(myStr) //Output: ‘ Removing White Space Both Ends ‘
console.log(myStr.trim()) //Output: ‘Removing White Space Both Ends’

9. trimStart() method

Same as trim but only for removing whitespace in the first end

let myStr = “ Removing White Space Both Ends “;
console.log(myStr.trimStart()) //Output: ’Removing White Space Both Ends ‘

10. trimEnd() method

Same as trim but only for removing whitespace in the last end

let myStr = “ Removing White Space Both Ends “;
console.log(myStr.trimEnd()) //Output: ’ Removing White Space Both Ends’

--

--