Ten Fundamental Things Of JavaScript
By: Mozumder Tushar
Tags: JavaScript, Javascript Frameworks, Javascript Development

Truthy & Falsy Values: In JavaScript when you use condition you have to know which one gives true value and which one gives falsy values. It is essential to know about truth and false values; otherwise, your condition might not give expected values.

Falsy Values: // 0, "", undefined, null, NaN let name = ""; if(name){ console.log("condition is true") }else{ console.log("condition is false") //Expected output } Truthy Values: // '0', ' ', [], {} let name = '0'; if(name){ console.log("condition is true") //Expected output }else{ console.log("condition is false") }

2. Null Vs Undefined: You can get undefined in many ways, Suppose you declare a variable but doesn't put a value there, there is no property in the object but you want to access it, you want to access an element on an array but that position is not there but in term of Null, it has to set explicitly.

let ages = [10, 15, 20, 45] console.log(ages[7]); //output: undefined

3. Double Equal Vs Triple Equal: Double Equal check the value only it doesn’t check type but Triple Equal check value and type as well.

Double Equal: const first = 2; const second = '2'; if(first == second){ console.log("condition is true"); //Expected Output }else{ console.log("condition is false"); } Triple Equal: const first = 2; const second = '2'; if(first == second){ console.log("condition is true"); }else{ console.log("condition is false");//Expected Output }

4.Block Scope: Block Scope means one doesn't read variable values outside of it a declaration, which means outside of the variable declaration you can't read it value. In blew code we can see, we can access results inside the sum function but when we use results outside of the function, it throws an error.

function sum(first, second){ let result = first + second; return result; } const output = sum(3,10) console.log(output); console.log(result); //Throw error

5. Map, Filter, Find: The map is used in terms of the loop, basically it is the simplest version of loop in JavaScript, it works the same as a loop, the same goes for Filter but it returns an array and finds return first match of the condition.

const students = [ { id: 1, name: 'X'}, { id: 2, name: 'Y'}, { id: 3, name: 'Z'}, ]; map:

const names = students.map( student => student.name ); console.log(names); //output: [ 'X', 'Y', 'Z' ] filter:

const names = students.filter( student => student.name ); console.log(names);

//output: [ { id: 1, name: 'X' }, { id: 2, name: 'Y' }, { id: 3, name: 'Z' } ] find:

const names = students.find( student => student.name ); console.log(names) //output: { id: 1, name: 'X' }

6. bind, call and apply: If there some object suppose x,y,z are object they have different value but common method says tax, In general, you have to write all this method all the three object but if you bind you could use one tax method for all of them but in term of call it can directly access the tax method, you only have to pass this argument (which one you want to use tax method) and the parameters to tax method, same as apply but here you have to pass the parameters as an array.

const x = { name: 'X',

salary: 100,

tax: function(amount){

this.salary = this.salary - amount;

return this.salary

}

} const y = { name: 'Y',

salary: 200,

} const Z = {

name: 'Z',

salary: 50,

} //bind

const xTax = x.tax.bind(y);

xTax(50);

console.log(y.salary); //output: 150 //call:

x.tax.call(y, 100)

console.log(y.salary);//output: 100 //apply

x.tax.apply(y, [60])

console.log(y.salary);

7. new keyword: In the previous example, we use three objects, those objects are similar but we write those object three times, to optimize that we can use the new keyword, to use the new keyword we have to use class components.

class Person { constructor(Name, salary) {

this.Name = Name;

this.salary = salary;

}

}

const x = new Person('X',100)

console.log(x); //output: Person { Name: 'X', salary: 100 } const y = new Person('Y',200)

console.log(y); //output: Person { Name: 'Y', salary: 200 } const z = new Person('Z',300)

console.log(z); //output: Person { Name: 'Z', salary: 300 }

8. Window: To build a website we use many things like history, document, location all of them are windows property, we can check them on console on the browser.

9.Function Scope: There are two types of function scope in JavaScript one is Local Scope another is Global Scope. When a variable is declared in a function then it is Local Scope to the function because they only access from that function, on the other hand when a variable declared outside of a function then it is in Global Scope, from all function, script on that page can access it.

//Local Function: //can't access Name



function myLocalFunction() {

const Name = "JavaScript";



// can access Name



} //Global Function: const Name = "JavaScript";



// can access Name



function myGlobalFunction() {



// can access Name



}

10. async-await: async is a function declared with the async keyword. Async functions are part of the JavaScript async function constructor, and the await is enabling within async. The async & await are asynchronously promise-based function, where when a promise is resolving it wait and work asynchronously. Usually, when developer use API’S to handle promises they use the async-await function

Thanks For Reading This Article.