Let’s Recap some js concepts

Md Abdullah All Naim
5 min readNov 5, 2020

--

truthy and falsy values:

truthy values mean the values that return true and falsy value means that returns false. well, let me explain by coding.

values that return true are : ‘0’, ‘ ’, {}, “false”

const value = ‘0’

if(value){

console.log(‘true’)

}else{

console.log(‘false’)

}

the result will be true

const emptyValue = ‘ ’

if(emptyValue){

console.log(‘true’)

}else{

console.log(‘false’)

}

the result will be true

const emptyObject = {}

if(emptyObject){

console.log(‘true’)

}else{

console.log(‘false’)

}

the result will be true

const falseAsString = “false”

if(falseAsString){

console.log(‘true’)

}else{

console.log(‘false’)

}

the result will be true

falsy values will return false if you check by above coding. they are false, 0, undefined, null

Null Vs Undefined:

We sometimes get confused when we will get the result null and unfined. Ok, take a deep breath and let’s destroy this confusion.

Different ways when you will get undefined :

  1. when you named a variable but didn’t assign any value to it, you will get undefined.

let name ;

console.log(name) — →> undefined

2. when you call a function who should receive some parameter when calling but you didn’t pass any parameter you will get undefined.

function addNumbers (a, b){

console.log(a + b)

}

console.log(addNumbers()) — →> undefined

3. if you do not set the property value for an object then you will get undefined.

4. let getUndefined = undefined

console.log(getUndefined) — →> undefined

two different ways when you get the result null

  1. let getNull = null

console.log(getNull) — →> null

2. when a element or variable does not exist you will get the result null

Double Equal Vs Triple:

double equal checks if the values of two data are same or not. It has no concern about the data types.

2 and ‘2’ are the same as the value.

if( 2 == ‘2’){

console.log( ‘true’)

}else{

console.log( ‘false’)

}

whereas triple equal checks if two data are same in type and same in value are not. 2 and ‘2’ are the same as data value but their types are not same.

if( 2 === ‘2’){

console.log( ‘true’)

}else{

console.log( ‘false’) — →> false

}

Be smart by using map, find and filter function:

map function will take three parameters. the first parameter will return the element from an array , the second parameter will return the index number and the third parameter will return the whole array. by using map you can play with array in a smarter way. because you can do whatever you want with the array. let me explain by coding.

const numArr = [ 1, 2, 3, 4 ]

numArr.map(( element, index, array) => {

console.log(element, index, array)

})

1 0 [ 1, 2, 3, 4 ]

2 1 [ 1, 2, 3, 4 ]

3 2 [ 1, 2, 3, 4 ]

4 3 [ 1, 2, 3, 4 ]

filter will return a portion an array if the array fill up some condtions.

const filterArr = [ 1, 2, 4, 7 ]

const result = filterArr.filter( x => x > 2 );

console.log(result) — →> [ 4, 7 ]

find function return element from an array who has fill the condition.

const mapArr = = [ 1, 2, 4, 7 ]

mapArr.find( x => x>5) — →> 7

Scope , block Scope , access outer Scope variable:

{

// codes inside curly braces can not be accessed from outside .

// this is called code block scope

}

{

{

// variables inside this curly braces cannot be accessed from outside this curly braces. but variable declared outside can easily be accessed from this area.

}

}

declaring variables using let, const cannot be accessed from outside of the block scope. so there is no risk of using the same name of variables in different places. For this , let , const are called block level scope.

whereas declaring variables using var can be accessed from outside the scope.For this var is called global level scope. so there is chance of overwriting any variables value so we should not use it in our projects.

window, global variable :

well, window keyword is used for giving permission to get the global level scope means whenever you declare anything using window keyword you can access them anywhere on your code. again writing window keyword or not the variable can be accessed from anywhere. This type of variable is known as a global variable.

document === window.document

console === window.console

(window.num = 12 ) === (num = 12)

new keyword and class keyword:

we all know about objects and we already have worked with objects. We have to declare the property name and its value. But when we need to create a lot of property and its value then it is not possible to write and time-dependent. So class keyword help us to solve this problem.

mainly class is a template used for creating objects. And the new keyword is used for calling the object and pass the property value. let’s see an example:

class Person {

constructor ( fName, lName, salary ){

this.fName = fName;

this.lName = lName ;

this.salary = salary;

}

}

const addPerson = new Person( ‘ Max’, ‘Well’, 2000 )

console.log( addPerson)

Let’s clear the concept of this keyword:

when you use this keyword inside the function declared inside any object, it represents the whole object. When you use this keyword for declaring a property name, then it represents that object property. let’s see an example:

const myObj = {

name : ‘Max’,

getFullName : function () {

return ‘Mr ’ + this.name;

}

}

console.log( myObj.getFullName()) — →> Mr Max

Iteration Vs Recursion:

iteration means code using loop whereas recursion means to code using condition inside a function and here function call itself . In recursion,there must be an initial condition or a stopping condition. It is really easy to use a cursive function.

Now let's practice some coding problems.

Find the largest number from an array ( without function):

const marks = [ 20, 78, 90, 85 ]

let maxValue = marks[0]

for ( let i = 0; i < marks.length ; i ++){

const element = marks[i]

if ( element > maxValue ) {

maxValue = element ;

}

}

console.log ( ‘Highest marks is : ’ , maxValue )

Calculate factorial in a recursive way:

function factorialOf (n){

if ( n == 0 || n== 1){

return 1;

} else {

return n*factorialOf(n-1)

}

}

console.log(factorialOf(5))

--

--