10 JavaScript topics that you should know.

Today I am going to talk about 10 JavaScript tricky topics that you must know for better learning JavaScript. I am excited. I think you are also excited. So, let’s start.

var, let, and const

here, ‘let’ and ‘const’ are block-level data type but ‘var’ is not a block-level data type. At first, I am explaining what is block here. When we write a curly bracket {} in our JS code, it is called a block. It can be written for ‘if’ clause or ‘for’ loop or function or anyone.

if (true) {
// execute something
}
for( i = 0; i < 5 ; i++) {
// execute something
}

If we want to declare a variable, which is only accessible in a block, then we should use ‘let’ or ‘const’. Otherwise, we have to use ‘var’. If we use ‘var’ to declare a variable, that will be accessible from anywhere in that code.

‘let’ and ‘const’ are the same category considering the accessibility area. But there is also much difference between ‘let’ and ‘const’. If we declare a variable using ‘let’, then we can update it. But if we use ‘const’, we cannot update it.

let a = 5;
console.log(a); // 5
a = 6;
console.log(a); // 6
const b = 5;
console.log(b); // 5
b = 6; // It will throw an error.

Difference between double-equals and triple equals:

Double equals don’t consider data type when comparing. As a result, if a number and that number’s string are compared, it will return true.

Triple equals consider data type when comparing. When the number and number’s string are the same in data but different in type, triple equals return false. See the code below for a better understanding.

const compare1 = ( 18 == '18');
console.log(compare1); // true
const compare2 = (18 === '18');
console.log(compare2); // false

How to access a property of an object?

There are basically two methods to access a property of an object. One is like a dot (.) operator and another is like an index method.

Dot (.) method:

const product = {
name: 'Camera',
for: 'Roky'
};
console.log(product.name);

Index method:

const product = {
name: 'Camera',
for: 'Roky'
};
console.log(product['name']);

What is NaN?

‘NaN’ means “Not a Number”. It is very disgusting. When we convert a string to a number and if there is anything without numerical in that string, then it will return ‘NaN’.

parseInt('Hello World', 10); // NaN

Here, we tried to convert the string ‘Hello World” to a decimal number. But “Hello World” cannot be converted into a number. So, it returns to ‘NaN’.

If we add a number with ‘NaN’, the result will also be “NaN”.

const a = NaN + 5;
console.log(a); // NaN

We can check whether a variable is ‘NaN’ or not using the ‘isNaN()’ built-in function.

a = NaN;
console.log(iSNaN(a)) // true
console.log(isNaN(5) // false
console.log(isNaN('I love Bangladesh')) // true

charAt() function:

We can find out any character in a string using its index.

const cat = 'Mini';
cat.charAt(1) // i

String Concat with number:

When we add a string to a number, the result will be a string. See the examples.

const a = '3' + 4 + 5;
console.log(a); // '345'
const b = 4 + 5 + '5';
console.log(b) // '95'

Here, in the first one, though 4 and 5 are numbers and they added as a string because of ‘3’. In the second one, at first 4 added with 5 and then 9 (sum of 4 and 5) added with ‘5’. As a result, the result will be ‘95’.

Add an item to the beginning of an Array

There is a way to push an element to the first of an array.

let people = ['Roky', 'Tamim', 'Mushfiq'];
people.unshift('Shakib');
console.log(people); // ['Shakib', 'Roky', 'Tamim', 'Mushfiq']

How to access the last element of an array?

Here, we can access the last element of an array.

const array = [4, 3, 8, 7];
console.log(array[array.length - 1]) // 7

JavaScript Random from 1 to 10

Here is a way to generate a random number from 1 to 10.

const a = Math.floor(Math.random() * 10) + 1;
console.log(a); // a random number from 1 to 10

How to sort an array in JavaScript

here is a way to sort an array.

var fruits = [4, 3, 8, 7];
fruits.sort();
console.log(fruits) // 3, 4, 7, 8

I am an enthusiastic JavaScript developer with knowing some framework for the frontend and the backend.