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

You may familiar or not with Javascript. It is also called Ecma Script (ES) JavaScript is a scripting language created in 1995 by Brendan Eich. At present we are using JavaScript/ECMA Script Version 6 (ES6) and the last update published in 2015. It is designed to run as a scripting language in a host environment like Browser. But it can be interpreted in server-side environments such as Node JS.
In the early 2000 Javascript litarely used in websites but at present, you can not think website without javascript.

JS Data Types

The Most Important thing you need to know in programing languages is Data Type. JavaScript has 7 Data types (BigInt is also a data type) they are
1. Number
2. String
3. Boolean
4. Symbol
5. Object
-Function
-Array
-Date
-RegExp
6. Null
7. Undefined

JS Variable

Variables are the containers for storing data values. Most of the programming languages have different variable declaration methods.
In Javascript, you can declare variable using

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

There is while, do…while, for, foreach, for…in -loop in JavaScript. Beginner js programmer may be surprised to know about for…in. I am explaining for…in here.

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

Strings are represented in text form. In another term String an object is used to represent and manipulate a sequence of characters. Some of the most-used operations on strings are to check their length.
Some regularly used string methods are charAt, concat, includes, endsWith, indexOf, lastIndexOf, replace, slice, split, startsWith, substr, toLowercase, toUppercase, trim, trimStart, trimEnd. Here I am discussing few methods below.

indexOf

The indexOf() method returns the index number of the specified value, starting the search at from the beginning of the sentence. Returns -1 if the value is not found.

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()

The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

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

The Number constructor contains constants and methods for working with numbers. Different values of other types can be converted using Number() function. Some common method using Number is isNaN, parseFloat, parseInt.

isNaN()

The isNaN() function determines whether a value is an illegal number (Not-a-Number).

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()

The parseFloat() function parses a string and returns a floating point number. It determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

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

An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this.
The most common using method is concat, every, filter, find, findIndex, forEach, indexOf, join, map, lastaIndexOf, pop, push, reduce, reverse, shift, slice, sort, splice, unshift. Here I am discussing few methods.

map()

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. Instead of manually iterating over the array using a loop, you can simply use the built-in Array.map() method.

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()

The slice() method returns the selected elements in an array, as a new array object.

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

--

--

MD. Rajibul Hasan | MERN Stack Developer

The Founder of Apps Maker BD and Frontend Developer at Penta Global Ltd.