Core-Concept-Of-JavaScript

Nasir Uddin
6 min readMay 8, 2021

Truthy & Falsy

What is Truthy value?

when you defined a variable right value and if those values are true or valid that’s called truthy value.

what is Falsy value?

If you define 0, false, empty string “”, undefined, null, NaN these value set for a variable you are getting falsy value.

Example Below:

let value = “Name”; //truthy

value = “”; //falsy

value = “ “; // truthy

value = []; // truthy

value = {}; // truthy

value = 0; // falsy

value = 1; // truthy

value = 1223; // truthy

value = undefined; //falsy

value = null; //falsy

value = NaN; //falsy

value = true; //truthy

if (value) {

console.log(“value is true and show truthy”);

} else {

console.log(“value is false and show falsy”);

}

Null Vs Undefined

Undefined

You can get undefined many times. If you are set variable but not defined value so you get undefined and if you use var variable declared and called this variable before initialize then you get undefined. When you declared a function and set parameter but you don’t pass parameter but you call the parameter you will get undefined

Example of Undefined:

let name;

console.log(name); // you get undefined

const myObj = { name: “hero”, age: 21 };

console.log(myObj.girlFriend); // you are get undefined

function myFunc(a, b) {

console.log(a, b);

}

myFunc(3); // here you get 3 from first parameter and second parameter return undefined

myFunc(); // both parameters are showing undefined because you don’t set any parameter in your function.

Null

If you don’t set your variable value can set manually null and you get or check on console null value.

Example Below:

let taka = null;

console.log(taka); // output null

Double Equal(==) VS Triple Equal(===)

Double Equal(==)

Double equal alway check the value and compare both value are similar or not similar then execute the result.

Example Below:

let fNumber = 4;

let sNumber = “4”;

fNumber = 0; // now out put is false

sNumber = true; // now out put is false

fNumber = 1; // now out put is true

sNumber = true; // now out put is true

fNumber = 0; // now out put is true

sNumber = false; // now out put is true

if (fNumber == sNumber) {

console.log(“these value are true”);

} else {

console.log(“these value are false”);

}

Triple Equal(===)

Triple equal always checks type and value and it’s a comparison of which value is similar to both values. If both variable types and values aren’t similar you get false. So developers must recommend using triple equal(===) to avoiding error and undefined.

Example Below:

let fNumber = 2; // now out put is false

let sNumber = “2”; // now out put is false

fNumber = 5; // now out put is true

sNumber = 5; // now out put is true

fNumber = 1; // now out put is false

sNumber = true; // now out put is false

fNumber = 0; // now out put is false

sNumber = false; // now out put is false

fNumber = 0; // now out put is true

sNumber = 0; // now out put is true

if (fNumber === sNumber) {

console.log(“You get true value”);

} else {

console.log(“You get false value”);

}

Global-Scop | Local-Scop | Block-Scop | Hosting

Global-Scop

Global scop variable is an outside variable in all function blocks if block loop block. This variable you can use anywhere or any block.

Example Below:

const title = “Mr.”;

function fullName(f, l) {

console.log(title);

return title + “ “ + f + “ “ + l;

}

console.log(fullName(“Nasir”, “Uddin”));

console.log(title);

Local Scop

when we declared a variable upper label to in function block if block and loop block. Then we can use this variable use the nested blocks any place. But we can’t use this variable out site this block.

Example Below:

function friend() {

const list = [“Jolil”, “Kholil”, “Molil”, “Jabbar”, “Gabbar”];

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

const element = list[i];

console.log(element);

console.log(“console in loop block”, list);

}

console.log(“console in the local block”, list);

}

friend();

console.log(“console outside of block”, list); // you will get ReferenceError not defined list.

Block Scop

If you are declared a variable in your loop block or if block or functional block you can access your variable inside of your block. Otherwise, you can’t access your variable outside of the block.

Example Below:

const array = [3, 4, 5, 6, 7, 8];

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

const element = array[i];

console.log(element); // you can get exact out put

}

console.log(element); // you get referenceError because you can’t access block scop variable outside.

Hoisting

When you use the var type variable and declared it in any block scop you can access your variable outside of your block scop. It’s called hoisting.

Example Below:

function getValue(n) {

var sum = 30;

if (n > 20) {

return sum + n;

} else {

return sum;

}

}

console.log(getValue(50));

Closure

when you use the function and inside return the function and count their value person by person and remember their previous value then recall these value it starts after previous value.

Example Below:

function counter() {

let count = 0;

return function () {

count++;

return count;

};

}

const count1 = counter();

const count2 = counter();

const count3 = counter();

console.log(“count1”, count1());

console.log(“count1”, count1());

console.log(“count1”, count1());

console.log(“count2”, count2());

console.log(“count2”, count2());

console.log(“count2”, count2());

console.log(“count1”, count1());

console.log(“count3”, count3());

console.log(“count3”, count3());

console.log(“count2”, count2());

Event Loop

The event loop is work by asynchronous methods. The first fall when javascript executes the code and use Stack data structure Last in First Out (LIFO) when you last code executes and compile this code is run time environment and then again you execute one more code like set time interval 2 seconds you again 1 more normal code execute first output show your normal execute code and then output setTimeInterval code. Because the JavaScript engine sends the setTimeInterval code event loop. When the event loop engine is finally completed the code execution event loop sends this code queue data structure. Finally, queue data structure uses the First in First Out (FIFO) method and sends this code from queue take this code stack data structure finally execution this code show output. Javascript single-threaded non-blocking asynchronous language.

Bind | Call | Apply

Bind Method

If you use the bind method first you have needed the function are contained in a variable and then call the variable shows the result. So if you use the bind method you have to needed two-step working.

Example Below:

const normalPerson = {

firstName: “Rahim”,

lastName: “Uddin”,

salary: 15000,

getFullName: function () {

console.log(`${this.firstName} ${this.lastName}`);

},

chargeBill: function (amount) {

console.log(this);

this.salary -= amount;

return this.salary;

},

};

const heroPerson = {

firstName: “Hero”,

lastName: “Balam”,

salary: 25000,

};

const friendlyPerson = {

firstName: “Hero”,

lastName: “Friend”,

salary: 20000,

};

const chargeBillHeroPerson = normalPerson.chargeBill.bind(heroPerson);

chargeBillHeroPerson(2000);

chargeBillHeroPerson(2000);

chargeBillHeroPerson(3000);

const chargeBillFriendlyPerson = normalPerson.chargeBill.bind(friendlyPerson);

chargeBillFriendlyPerson(2000);

chargeBillFriendlyPerson(2000);

chargeBillFriendlyPerson(1000);

chargeBillFriendlyPerson(1000);

Call Method

Call methods are called directly function value and show output. when using the call method first you have to needed (this.) value and set comma uses a number of parameters how much you defined.

Example Below:

const normalPerson = {

firstName: “Rahim”,

lastName: “Uddin”,

salary: 15000,

getFullName: function () {

console.log(`${this.firstName} ${this.lastName}`);

},

chargeBill: function (amount, tips, tax) {

console.log(this);

this.salary -= amount + tips + tax;

return this.salary;

},

};

const heroPerson = {

firstName: “Hero”,

lastName: “Balam”,

salary: 25000,

};

const friendlyPerson = {

firstName: “Hero”,

lastName: “Friend”,

salary: 20000,

};

normalPerson.chargeBill.call(heroPerson, 3000, 300, 300);

console.log(heroPerson.salary);

normalPerson.chargeBill.call(friendlyPerson, 4000, 400, 40);

console.log(friendlyPerson.salary);

Apply Method

Apply method are same as Call method. call method follows comma by the separate passing parameter value. And Apply method use comma after setting (this.) value and passing all parameter in one Array and same as to call separate all parameter value in the list of Array.

Example Below:

const normalPerson = {

firstName: “Rahim”,

lastName: “Uddin”,

salary: 15000,

getFullName: function () {

console.log(`${this.firstName} ${this.lastName}`);

},

chargeBill: function (amount, tips, tax) {

console.log(this);

this.salary -= amount + tips + tax;

return this.salary;

}

}

const heroPerson = {

firstName: “Hero”,

lastName: “Balam”,

salary: 25000

}

const friendlyPerson = {

firstName: “Hero”,

lastName: “Friend”,

salary: 20000

}

normalPerson.chargeBill.apply(heroPerson, [3000, 300, 300]);

console.log(heroPerson.salary);

normalPerson.chargeBill.apply(friendlyPerson, [4000, 400, 40]);

console.log(friendlyPerson.salary);

--

--