If you know basic javascript then this blog will help you to understand some tricky things in javascript. So let’s start

In javascript when we say truthy value it doesn’t mean that value is true but it means that forcefully value becomes true in a boolean contex. Let’s see an example then we will understand it clearly.

    if (1) {
console.log("Truthy value!");
} else {
console.log("Falsy value.");
}

If we run the above code we will get output: “Truthy value !” because in the parameter of if() I pass value 1 which is a truthy value that’s why this if() parameter become true and under the if blocks executes. Without false, 0, -0, 0n, “”, null, undefined, and NaN all values are called truthy values.

Opposite of truthy values is falsy values. In javascript when we say falsy value it doesn’t mean that value is false bt it means that forcefully value becomes false in a boolean contex. Let’s see an example then we will understand it clearly.

    if (0) {
console.log("Truthy value!");
} else {
console.log("Falsy value.");
}

After running the above code we will get output: “Falsy value.” because in the parameter of if() I pass value 0 which is a falsy value that’s why this if()parameter become false and under the if blocks don’t executes. Let’s see some falsy values : false, 0, -0, 0n, “”, null, undefined, and NaN all values are called falsy values.

To understand the difference between null and undefined let’s see an example first

var Test;
console.log(typeof Test); //Output: undefined

I just declare a variable called Test after declare if we check the type of Test variable we will get undefined .

const Var=null;
console.log(typeof Var); //Output: object

I just declare another variable called Var and initialize a value null then checked it’s type we will get output: Object

So from the previous two example we can surely say that undefined and null are two different type data.

The both are comparison equality operators

Double equal(==) is a comparison operator which converts operand to be same type before comparison. On the other hand Triple equal(===) is a strict equality comparison which checks both the value and type of operands. Let’s see an example

0==false   //Output: true  :because false is equal to 0
0===false //Output: false :because boths are different type
2=="2" //Output: true :auto type conversation, string -> number
2==="2" //Output: false :since both operands are different type

It is a Array method which creates a new array. It is used to iterate an array elements.

Syntax:

const newArray= array.map(callback(currentValue[, index[, array]]) {
// return element
}[, thisArg]);

Parameters: It takes two parameters one is callback function and another is thisArg(optional). One important thing is that it doesn’t change the original array.

Let’s se an example of this map() method

let array = [1, 2, 3, 4];
let newArray= array.map(x => x * 2);
console.log(newArray);
//output: Array [2, 4, 6, 8]

It is also a Array method . It returns a new array base on the condition we provide in the callback function.

Syntax:

array.filter(callback(element,index,arr),thisArg)

Parameters : It takes five parameter. They are callback, element, index, array, thisArg.

Let’s se an example of this filter() method

let numbers= [30, 35, 18, 45];
const result = numbers.filter(number=> number> 20);
console.log(result);
// expected output: Array [30,35,45]

In general there are two type of scope one is global and another is local scope. Let’s see an example of local scope :

console.log(carName,"---in outside the car function");
//output: error: Uncaught ReferenceError: carName is not defined
function carFunction() {
let carName = "BMW";
console.log(carName,"---in the car function");
}
carFunction();

what is happening in the above code we are trying to get the value of carName variable but we are getting error because carName variable is declared in carFunction so it’s scope is only in the carFunction . Outside the carfunction we can not use carName variable.

Let’s see an example of global scope :

let carName = "BMW";
console.log(carName,"---in outside the car function");
//output: error: Uncaught ReferenceError: carName is not defined
function carFunction() {

console.log(carName,"---in the car function");
}
carFunction();

If we execute this function we can see that carName variable is declared at the top and inside the carFunction we can access the carName. This is called global scope.

I will show you how to reverse a string in different way. One is using built in function and another is using normal for loop

Using built in functions:

function reverseStringFun(string) {

let splitString = string.split("");

let reverseArray = splitString.reverse();

let Array = reverseArray.join("");

return Array;
}
reverseStringFun("helloFahim");
//Output: mihaFolleh

using for loop :

function reverseStringFun(string) {
var newstring = "";
for (let i = string.length - 1; i >= 0; i--) {
newstring += string[i];
}
return newstring;
}
reverseStringFun('helloFahim');

Using reduce() method :

    const array = [1, 2, 3, 4, 5,6,7,8,9,10];

// Getting sum of numbers
const sum = array.reduce(function(a, b){
return a + b;
}, 0);

console.log(sum);
// output : 55

Using normal for loop :

const array = [1, 2, 3,4,5,6,7,8,9,10];
let totalNumber=0;
for (let i=0,l=array.length; i<l; i++) {
totalNumber+=array[i];
}
console.log(totalNumber)
// output : 55

Finding max using buit in function :

const array = [1,3,2,5,15,7,8,9,4];console.log(Math.max(...array));
// expected output: 15

Finding min using buit in function :

const array = [3,2,5,15,1,7,8,9,4];
console.log(Math.min(...array));
// expected output: 1

Engineer| Web Developer| Programmer