JavaScript For Fun: Some Tricky Tropics In Interview

MD, Nahidul Islam Eraz
3 min readMay 8, 2021

We will discuss some of the JavaScript topics that are tricky & important in the interview question.

JavaScript has one of the most demanding and vast job sectors in the present IT Department. Let’s see some common, important, and tricky JavaScript tropics that are often asked in the interview. Let’s check it out.

Truthy & Falsy Value

Any number value except 0 then javascript auto-tuned it into true. If the value is 0 then it will count as false. Same as for string. All values except null count as true and if the string value is null then it will count as false. Space “ “ also counts as true value. If we don’t define variables then it will also count as a false value.

Null vs. Undefined

When undefined happened :

  • If we don’t declare variables.
  • No existence of the property, even then we try to access it.

When null happened :

  • When we explicitly set inside an element that there is no value. Null express the lack of identification, indicating that a variable point to no object.

Double Equal vs Triple Equal

Double equal (==) is basically used to check the only value. It doesn’t care that the value is numeric or string. If two value matches it will make it true.

const num1 = 2;const num2 = “2";num1== num2=>true

On the other hand Triple equal (===) will check both value and type. If values matches but typer are different it will return false.

const num1 = 2;const num1 = “2";num1=== num2=>false

Scope

Scope means what variables you have to access inside the JavaScript. There are two types of scope. One, local scope and two global scopes.

Variables that are accessible only in the specific part of the program that is called local scope.

const moreNum = 10;function number(num1, num2){   const add = num1+num2;   console.log(add)   return add;}const total = number(1,2);console.log(moreNum);console.log(total);

So, in here add is a local variable. Its limitation is in the function. We can’t call it outside the function.

On the other hand, moreNum is the global scope. The global scope can be called anywhere of the program. Either inside the function or outside. It doesn’t matter.

But here is a thing those things only happened when we use to let or const. If we use var those things won’t happen. For more detail, we will need to understand ES6 Block Level Declaration.

Closure

If we call or return a function from another function then it will make a closed environment and it can call a global scope then it captures its own value. That’s called closure.

function addValue(){     let add = 0;     return function(){         add++;         return add;     }}const value=addValue();const moreValue = addValue();console.log(value());console.log(value());console.log(moreValue());console.log(moreValue());

Bind

Bind can take a method from another object and create a new function and use it by itself.

alise.chargeBill.bind(jack);jackBillCharge(1000)

Call

Call is used to call a method from an object and apply it to another object.

alise.chargeBill.call(jack,1000);

Apply

Apply’s working procedure is the same as call but in this case, we call the parameter with array.

alise.chargeBill.apply(jack,[1000]);

Event Bubble

When an event happened on an element it dives down from parent to end of child component then it will go upwards and check the event handler. If exists then it will execute and again go to the top and stop at their ancestors.

function handleInput(e) {
// Checks whether the event bubbles and ...
if (!e.bubbles) {
// ... passes the event along if does not
passItOn(e);
}

// Already bubbling
doOutput(e);
}

Event Delegation

Event delegation is a method where to put an event handler into its parent to try to avoid adding an event handler onto its multiple child components.

Benefits Of Event Delegate

  1. Attach events at the parent element so need to add event handler once
  2. less event handler setup because no need for adding an event handler for the child components
  3. no need to set up an event for newly added elements.

That’s it for today. We will discuss more tropic later.

--

--

MD, Nahidul Islam Eraz

I am a front-end developer who loves coding, feel joy to learn, just need a cup of tea and ready for new journey.