Intermediate JavaScript Concepts

JavaScript is a broad language but there are some common things that are often asked in the interview. Today I will discuss some intermediate javascript concepts with you.

Image for post
Image for post
  1. Truthy and Falsy values:

When the value is Number.

If the value of a variable is greater than zero then if-statement will return it as true and if the value of a variable is zero then the if-statement will return it as false. For example:

const numbers = 12

if (numbers) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is true’

const numbers = 0

if (numbers) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is false’

When the value is a string:

If a string is declared in a variable, the if-statement will return it as true but if an empty string is declared in the variable, then if-statement will return false. For example:

const Hero = “Salman Shah”

if (Hero) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is true’

const Hero = “”

if (Hero) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is false’

Again, if you declare an empty string in a variable with white-space in if-statement, it will return true. check below example:

const Hero = “ “

if (Hero) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

Another example:

let Hero;

if (Hero) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is false’

More Examples:

let Hero = NaN

if (Hero) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is false’

Finally, you can say that if the value of a variable is zero or it is an empty string or it is undefined or it is NaN then the if-statement will return it as false.

2. Null Vs Undefined:

If you declare a variable and do not set its value and want to see its output on console then it will return undefined. For example:

let hero;

console.log(hero)

// output is ‘undefined’

Again, if you do not return a function by declaring it or you write return but do not set the value then if you call that function it will return undefined. Let's see another example:

const add = (num1, num2) => {

console.log(num1 + num2)

return

}

const result = add(3, 5)

console.log(result)

// output is ‘undefined’

Similarly, if you pass two parameters in a function that you declare and use one parameter when calling that function then it will return undefined. look at the below example:

const add = (num1, num2) => {

console.log(num1)

}

const result = add(3)

console.log(result)

// output is ‘undefined’

The same goes for the object. if you call the unset property in an object, the return will become from the object is undefined. Check out my below example:

const hero = {

name: ‘Shalman Shah’,

age: 28,

phone: 012547

}

console.log(hero.address)

// output is ‘undefined’

Null: Nul is the value of an object that was previously set but is not being set now.

3. double equal (==) vs triple equal (===):

The difference between double equal and triple equal is that double equal checks only the value of the statement and triple equal checks value with types. For a detailed example, see the below code:

const a = 5

const b = ‘5’

if ( a == b) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is true’

const a = 5

const b = ‘5’

if ( a === b) {

console.log(‘condition is true’)

} else {

console.log(‘condition is false’)

}

// Output is ‘condition is false’

4. bind:

To use a method by calling a method from one object to another object, you need to use bind. Bind actually does, calling a method from within an object, binding to it, and taking another object as a parameter in it, and the first object that has declared method takes this method to apply another object and return a function. See below code example:

const normalPerson = {

firstName: ‘Ringku’,

lastName: ‘Rahman’,

salary: 30000,

chargeBill: function (amount) {

this.salary = this.salary — amount

return this.salary

}

}

const bankPerson = {

firstName: ‘Rohim’,

lastName: ‘Uddin’,

salary: 25000

}

const bankPersonBill = normalPerson.chargeBill.bind(bankPerson)

console.log(bankPersonBill(800))

// output is ‘24200’

5. call:

The call is doing the same thing as bind is doing but the difference between bind and call is, the call takes the parameter of the method as a parameter without return any function. Look at the below code:

const normalPerson = {

firstName: ‘Ringku’,

lastName: ‘Rahman’,

salary: 30000,

chargeBill: function (amount) {

this.salary = this.salary — amount

return this.salary

}

}

const bankPerson = {

firstName: ‘Rohim’,

lastName: ‘Uddin’,

salary: 25000

}

const bankPersonBill = normalPerson.chargeBill.call(bankPerson, 800)

console.log(bankPerson.salary)

// output is ‘24200’

6. apply:

Apply exactly do the same thing that call is doing but the simple difference is that apply takes parameters into an array. For example, see the below code:

const normalPerson = {

firstName: ‘Ringku’,

lastName: ‘Rahman’,

salary: 30000,

chargeBill: function (amount) {

this.salary = this.salary — amount

return this.salary

}

}

const bankPerson = {

firstName: ‘Rohim’,

lastName: ‘Uddin’,

salary: 25000

}

const bankPersonBill = normalPerson.chargeBill.apply(bankPerson, [800])

console.log(bankPerson.salary)

// output is ‘24200’

Written by

Self-taught frontend developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store