You need to Know About JavaScript

I will explain some basic things about Javascript and also some Interesting code examples you need to know!

Introduce with JavaScript

JS Data Types

JS Variable

var: It is a traditional and old way of JS variable declaration
Eg. var a=5;

let: It is a block level variable.
Eg. let a=5;

const: You can declare one variable name in the same script once a time
Eg. const a=5;

JS Loop

for…in: you can break an object through this

const object = { a: 2, b: 3, another:4};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}

// Output:
// “a: 2”
// “b:3”
// “another: 4”

JS String

indexOf

Example
const paragraph = ‘The quick brown fox jumps over the lazy dog. The question is, really the dog was lazy?’;
const searchKey = ‘dog’;
const indexOfFirst = paragraph.indexOf(searchKey);

console.log(`The index of the first “${searchKey}” from the beginning is ${indexOfFirst}`);
// expected output: “The index of the first “dog” from the beginning is 40"

console.log(`The index of the 2nd “${searchKey}” is ${paragraph.indexOf(searchKey, (indexOfFirst + 1))}`);
// expected output: “The index of the 2nd “dog” is 73"

substr()

Example
const str = ‘Rajibul Hasan’;

console.log(str.substr(1, 2));
// expected output: “oz”

console.log(str.substr(2));
// expected output: “jibul Hasan”

Js Number

isNaN()

Example
function checkIsNan(param) {
if (Number.isNaN(param)) {
return ‘The number NaN’;
}
if (isNaN(param)) {
return ‘NaN’;
}
}

console.log(checkIsNan(‘100F’));
// expected output: “NaN”

console.log(checkIsNan(NaN));
// expected output: “Number NaN”

parseFloat()

Example
function useParseFloat(r) {
if (Number.isNaN(Number.parseFloat(r))) {
return 0;
}
return parseFloat(r) * 2.0 * Math.PI ;
}

console.log(useParseFloat(‘xyz’));
// expected output: 0

console.log(useParseFloat(‘10.329abcd’));
// expected output: 64.89902103785795

Js Array

map()

Example
const array1 = [12, 14, 9, 18];
const mymap = array1.map(x => x * 2);

console.log(mymap);
// expected output: Array [24, 28, 18, 36]

slice()

Example
const allAnimals = [‘parrot’, ‘ant’, ‘goat’, ‘duck’, ‘horse’];

console.log(allAnimals.slice(2));
// expected output: Array [“goat”, “duck”, “horse”]

console.log(allAnimals.slice(2, 4));
// expected output: Array [“goat”, “duck”]

console.log(allAnimals.slice(1, 5));
// expected output: Array [“ant”, “goat”, “duck”, “horse”]

I hope you will be benefited from this article, If you have any questions and suggestions about this article please comment below.
Thank you.

You can visit my website rajibulhasan.me

I am a curious Programmer and Full-Stack Web Developer. My passion is coding and love to share my knowledge and experience with others.