JAVASCRIPT IMPORTANT THINGS YOU NEED TO KNOW

Suhrav Hussain
5 min readMay 8, 2021

1.TRUTHY AND FALSY VALUES

Javascript has two kinds of boolean values. true and false. Today we will talk about whose value is truthy and whose falsy. Let’s talk about truthy value. If we set a variable value true obviously his value will be truthy. Any kind of number without 0 is truthy value. Any string without an empty string is truthy value, even if we put only a space in a string it will be truthy. An array or object is a truthy value. truthy value example:

// var value =‘string’ 
// var value = ' '
// var value = 27
// var value = -299
// var value = {}
// var value = []
// var value = true

Now let's talk about falsy values. If a set a variable value false it will be falsy value. An empty string is falsy. 0 number is falsy. null , undefined , NaN is falsy value . falsy values example:

var value = ‘’
var value = 0
var value = undefined
var value = null
var value = NaN
var value = false

2. NULL VS UNDEFINED

Undefined is which is not defined. whenever we declare a variable but don't define its value it will return undefined. when we try to get something from a function that is not returned from the function will be undefined. Even when we try to get property’s value from any object or array which doesn't exist then we will get undefined . If we don't a function’s parameter value it will return undefind. undefined example:

 let value
console.log(value) // output undefined
function sum(a,b){
sum=a+b}
console.log(sum(23,45)) // output undefined

Null indicates an empty value. We can explicitly set any variable value null so that it doesn't return undefined.The typeof null is an empty object

let a = null
console.log(a)// null
console.log(typeof(null)) // empty object

3. ‘==’ VS ‘===’

If we try to compare two different types of value with double equal the it will convert any one type into the same type of another value.It means if we try to compare a string type and a number type then the double equal converts any type to match the other. exapmle:

let a= '100'
let b= 100
if(a==b)console.log('true') //true

Triple equal is more strict, it won't convert anything. If the type don't match it will return false. example:

let a = ‘100’
let b = 100
if(a===b)console.log('true') // false

4. SCOPE, BLOCK

Scope is from where we can access a variable. Sometimes we can access a variable from anywhere in the code. Sometimes we can’t access a variable from outside of a function or block.

What is JavaScript block?

JavaScript block is whenever we declare a function or if else statement or any loop it creates a block for its own. There are three kind of scope. Global scope, local scope and outer function scope. A variable declared in global scope can be accessed from anywhere. Variable declared in local scope can’t be accessed from outside of the block. A function can access his outer function scope or globao scope but can’t access his inner function scope. But a variable declared without any declaration keyword can be accessed from anywhere became i’s of hoisting . Example :

var a = 100

function add(a, b){

Let sum = a+b

Rerun sum}

add(7,8)

console.log(a) // 100

console.log(sum) // error

5.CLOSURE

Closure is actually a closed environment. When a function has access to outer function’s variables called closure. Every we create a function it will create its own closure which will preserve its own data. Example :

function outer(){

Let value=100

return function(){

value++

console.log(value)

}

}

console.log(outer()) // 101

6. BIND Vs CALL Vs APPLY

If we want to use any object’s methods or properties in any function we can use bind, call or apply method. But there are little bit difference. Bind method won’t invoke the function immediately. It will return a function with those methods or properties. Call and apply are almost same. The call() method takes arguments separately. The apply() method takes arguments as an array.

Example:

const person = {name: "shakib khan"}

function showInfo(age){
return `${this.name} is ${age}`
}

console.log(showInfo.bind(person, 15)) // output function
console.log(showInfo.call(person, 15)) // output shakib khan is 15
console.log(showInfo.apply(person, [15])) // output shakib khan is 15

7. THIS KEYWORD

JavaScript this keyword represents the from whose context its getting called. If we define a method in an object with this keyword and use it from this object then the this will represent its own object. But if we borrow any method from another object and use it in an object then the this keyword will represent the object which borrowed it.

Imagine I borrowed money from mr.x and then gave it to mr.y. So who has the money? Mr.x but in mr.y perspective who has the money? Me.

Example:

const mrX={

Name:’ Mr.x’,

Money: 20,

function givemoney(){

console.log(‘money give from ’+ this.name)

}

}

const me = {

Name: ‘me’,

Money: mrX.Money,

Givemoney = mrX.givenmoney

}

console.log(me.Givemoney())// 20 give from me

8.DOM

DOM is Document Object Model. We know that html give a structure to website. Html is a document. Every html works in browser as an object.browser understand them as an object. And html runs like a tree format in the browser, which is model tree. From this DOM word came. Without DOM javascript Won’t understand the html elements in browser and can’t work.

9. VAR vs LET vs CONST

javascript has 3 keywords for variable declaration. Var, Let and const. The main difference between var and let/const is their scope. If we declare var outside of a function scope then it will become global. On the contrary Let and const are not function scoped they are block scoped. Means the won’t be available outside of a block. Difference between Let and const is we cant reassign const value.

10.API

Api is a path which through we can get data from back end or save data in back end. Basically It’s link to database.when we will call the link and order them to get data or save data it will perform in that way.

--

--