DEV Community

Cover image for JavaScript tricky interview questions
SABBIR AHMMED
SABBIR AHMMED

Posted on • Updated on

JavaScript tricky interview questions

What are the truthy and falsy values of javascript?

JavaScript falsy means false of boolean condition context. Mainly, six expressions are called falsy. They are false, ‘’(empty string), undefined, null, NaN and 0. And the rest of the expression treats truthy. For example –

let value = NaN;

if(value) {
    console.log( “Truthy value”);
} else {
    console.log( “Falsy value” );
}
Enter fullscreen mode Exit fullscreen mode

Output: False value, because here boolean condition expression is false.

What is the difference between null and undefined?

null and undefined both are reserve keywords of javascript. In javascript null is used to assign an empty value, that’s means nothing. On the other hand, if we declare a variable but not define it yet at that time, the variable will show undefined. For example –

let value;
console.log( value );     // undefined
Enter fullscreen mode Exit fullscreen mode

Else, if we don’t return anything from the function, it will show undefined.

What is the difference between == and === ?

Double equal ( == ) is used for comparing two variables, but it doesn't check their data types. If one is an integer and another is a string but both contain the same value then it will be true. For example — -

let x = 4 ;
let y = “4” ;
if ( x == y ) {
    console.log( “ condition is true “ );
}
Enter fullscreen mode Exit fullscreen mode

But, three equal not only compare two variables but also check their data types. If data types are the same both, so they are truthy.

Define scope and block scope

The scope is just like the area. One is global scope and another is local scope. In general, when we declare a variable following by ES6 rules such as let and const in the function that variable is called function scoped variable or local variable. But if we want to declare a variable outside of a function is called global variable and its access is everywhere. For example –

const x = 15       // this is global variable or global scope

function doSomething() {
    const y = 15 ;           // local or function scoped variable
    const sum = x + y ;     // the variable which are inside of 
    return sum;            // curly braces are called block scope variable
}

console.log( doSomething() ); // 30
Enter fullscreen mode Exit fullscreen mode

Noted that, you can’t access y and sum variables outside of doSomething() function. The scope of y and sum are only in doSomething().

let and const are called block scope keywords. You can’t access outside of block curly braces { }. And var is a keyword that is called function scope variable.

What is hoisting?

Hoisting in a JavaScript's default behaviour. In javascript, var is a keyword that is used to declare variables. This var allows hoisting, hoisting means you can access a variable from anywhere in the parent scope. Hoisting set a reference of variable in the global scope or immediate parent scope. But doesn’t carry assigned value. For example –

const playFootball = () => {

    console.log("gameName hoisting : ", gameName );     // undefined

    let rain = true;

    var gameName = "Football";

    console.log("status hoisting : ", status );       // undefined

    if( rain ) {
        var status = "continue game" ;
    }

    console.log("status : ", status );               // continue game

}

playFootball();
Enter fullscreen mode Exit fullscreen mode

NB : const, let and arrow function do not support hoisting

What is Closure in JS?

The closure is a hot topic of JS. I’m going to discuss this here briefly. In JavaScript, closures are created when the inner function is created inside of a function. That inner function holds the reference from its parent function scope. For example –
Alt Text
Here, num2 is used by the inner function that time closure appears. And num1 comes from global scope, global variables are always reserved and any function can use them. But a variable in the function when it is used by an inner function that time closure is created.

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function.

Discussion (5)

Collapse
eissorcercode99 profile image
The EisSorcer

Which job role or position asks such easy questions? Hope you got the job btw very concise answers!!

Collapse
sabbir185 profile image
SABBIR AHMMED Author

This article is beginner friendly. If you are an expert person just ignore this. Advance JS questions are coming soon. Thanks

Collapse
eissorcercode99 profile image
The EisSorcer • Edited

totally not an expert. I'm completely a n00b. I literally just started coding as of last year and just completed "learn JavaScript" on Codecademy, but seriously. What job asks such questions because perhaps it means I can leave my current job for a job in tech now!?

Thread Thread
oadrian2 profile image
oadrian2

Generally for people coming straight out of college. We asked a couple of these and you'd be surprised how many candidates didn't actually know them.

Thread Thread
eissorcercode99 profile image
The EisSorcer

got any openings?