Some Javascript tricks solved problem’s
By: Asit Kumar Dey
Tags: Problem Solving, JavaScript, Programming, ES6

Some Javascript tricks solved problem’s

Null vs Undefined:

Null:

The Null is used to represent an intentional absence of value. It represents a variable whose value is undefined. It accepts only one value, which is null. The Null keyword is used to define the Null type in TypeScript, but it is not useful because we can only assign a null value to it.

var myVar = null; console.log(myVar); // null

Undefined:

It is very important to know about undefined coding in JavaScript. There are many ways to get undefined described below.

Undefined is found if you do not set the value of any variable.

let banana; console.log(banana) // undefined

If we do not declare a return in a function, then undefined will be found.

function sum(price1, price2){ console.log(price1 + price2) } const result = sum(34, 12); console.log(result); // undefined

when we write the return but do not give the value of the return then it is undefined.

function sum(price1, price2){ console.log(price1 + price2) return } const result = sum(34, 12); console.log(result); // undefined

when you do not pass any function parameters, you will get undefined.

function sum(price1, price2){ console.log(price1 + price2) return sum } const result = sum(34,); console.log(result); // 34, undefined

When declaring a variable, if you read the property of an object, it is undefined.

const firstProduct = {name:”computer”, price:1200}; console.log(firstProduct.quantity) // undefined

When declaring a variable, if you read the property of an object, it is undefined.

let funCode = undefined console.log(funCode) // undefined

2. Double equal (==) vs Triple equal (===):

These two type operators are…

Double equals operator (== ): Known as the equality or abstract comparison operator

Triple equals operator (===): Known as the identity or strict comparison operator

‘==’ converts the variable values to the same type before performing the comparison. This is called type coercion. Meanwhile, === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

example,

var number= 2;

var number1 = 2;

var string = "2"; console.log(number == number1); // true

console.log(number === number1); // true

console.log(number == string); // true

console.log(number === string); // false

Problem Solving:

Reverse a string

// Using for loop with variable decreament from last to first

function reverseStr(str) {

let newStr = "";

for (let i = str.length - 1; i >= 0; i--) {

newStr += str[i];

}

return newStr;

}console.log(reverseStr('Bangladesh'));

// hsedalgnaB

Calculate Factorial of a number using for loop

function factorial(num) {

// 0 or 1 factorial is always 1 if (num === 0 || num === 1)

return 1;

for (let i = num - 1; i >= 1; i--) {

num *= i;

}

return num; }

console.log(factorial(5));

// 120

Remove duplicate item from an array

// Using Set method

let names = ['Asit', 'Kumar', 'Sam', 'Asit', 'Kumar', 'Nusrat']; let uniqueNames= [...new Set(names)]; console.log(uniqueNames);

// ['Asit', 'Kumar', 'Sam', 'Nusrat']

Find the largest element of an array

const arr = [1, 5, 7, 3, 11, 9, 99, 57];let largest= 0; for (i = 0; i <= largest; i++){

if (arr[i]>largest) {

largest=arr[i];

}

} console.log(largest); // 99

Calculate Factorial of a number using a while loop

function factorial(num) {

let result = num;

if (num === 0 || num === 1)

return 1;

// When num is less than 1, looping is stopped

while (num > 1) {

num--;

result *= num;



}

return result;

}

factorial(5);

// 120

Calculate Factorial in a Recursive function

// factorial function runs itself until finished

function factorial(x) {



if (x === 0) {

return 1;

}

return x * factorial(x-1);

}

console.log(factorial(5));

// 120

Check whether a number is a Prime Number or not

// Using ES6 syntax

const isPrime = num => {

for(let i = 2; i < num; i++) {

if(num % i === 0)

return false;

}

return num > 1;

}

console.log(isPrime(10));

// falseconsole.log(isPrime(5));

// true

Gain Access to Expert View — Subscribe to DDI Intel