A Day with JavaScript basic concept
By: Murad Hossain
Tags: JavaScript, Object Oriented, Arrays, Javascript Tips

Building blocks of JavaScripts:

Number

String

Boolean

Object

Function

Array

Date

RegExp

Symbol (new in ES2015)

null

undefined

Number:

JavaScript Number is a primitive wrapper object used to represent and manipulate numbers like 52 or -8.04. The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#.

To convert a String or others value to the Number type we used a function: Number(value).

Example: Using Number to convert a date object

let d = new Date(‘January 10, 2020 02:52:00’)

console.log(Number(d))

String:

Strings in JavaScript are sequences of Unicode characters. Each Unicode character is represented by either 1 or 2 code units.

Some of the most-used operations on strings are checked their length, to build and concatenate them using the + and + = string operators, checking for the existence or location of substrings with the indexOf() method, or extracting substrings with the substring() method.

Two ways of access an individual character in a string:

return ‘car’.charAt(1); //a

return ‘car’[1]; //a

Array:

Array in JavaScript is actually a list-like object.

Example:

let fruits = [‘Apple’, ‘Orange’, ‘Banana’];

console.log(fruits.length); //3

Add an item to the end of an array: fruits.push(‘Pineapple’);

// [‘Apple’, ‘Orange’, ‘Banana’, ‘Pineapple’];

Remove an item from the end of an array: fruits.pop();

// [‘Apple’, ‘Orange’, ‘Banana’];

Find the index of an item in the array:

fruits.indexOf(‘Banana’); // 2

Accessing Array Elements:

JavaScript arrays are zero-indexed. The first element of an array is at index 0, and the last element is at the index value equal to the value of the array’s length property minus 1.

let items = [‘first’, ‘second’, ‘third’];

console.log(items[0]); // ‘first’

Objects:

An object is a collection of properties, and a property is an association between a name and a value. An object has properties associated with it.

let person = new Object();

person.name = ‘Murad’;

person.age = 30;

We can also create the above object using object initializer

let person = {

name: ‘Murad’,

age: 30,

}

Operators:

In JavaScript, an operator is a special symbol used to perform operations on operands.

Example: x = x + 4;

JavaScript Numerics operators are +, -, *, %, and /.

Operators types:

Assignment Operators.

Arithmetic Operators.

Comparison Operators.

Logical Operators.

Bitwise Operators.

String Operators.

Other Operators.

Function:

A function allows you to define a block of code, give it a name and then execute it as many times as you want. Functions are the core component in understanding JavaScript.

// defining a function

function <function-name>() {

// code to be executed

}

// calling a function

<function-name>();

Scope:

Scope in JavaScript defines accessibility of variables, objects and functions.

Scope types:

Local Scope.

Variable declared inside any function.

let name = ‘Karim’;

function person() {

name = ‘Araf’;

}

Global Scope.

Variable declared outside of any function.

function person() {

let name = ‘Araf’;

}

Closures:

An inner function always has access to the vars and parameters of its outer function, even after the outer function has returned.

function outerFunction() {

let myValue = 4;

function innerFunction() {

alert(myValue);

}

innerFunction();

}

Null and undefined:

Null: A null means absence of a variable.

let myVar = null;

alert(myVar); // null

Undefined: A variable or object has an undefined value when no value is assigned.

let myVar;

alert(myVar); // undefined