Some Question And Answers That You Should Know As A JavaScript Developer.

Are you going to face an interview as a JavaScript developer? but have no idea about what type of questions you have to answer? Then don't be afraid. We are here to tell you about the 10 most asked question in an interview and their solution.

What Do You Know About Truthy and Falsy Values?

As a programming language javaScript always analyze code in its own way. The first topic that we are gonna describe is when a value is counted as false and when it's true.

When JavaScript counts a value as false:

  1. When we declared a number type variable with value = 0 then Javascript counts it as a false value and also the condition becomes false. Instead of giving 0 as a value whatever you will set (negative values are also allowed") will count as a true value and also the condition will be true.

  2. If you declare an empty string ("") then Javascript counts it as a false value and also the condition becomes false. Instead of declaring an empty string whatever you will set will count as a true value and also the condition will be true. Even if you declare a string only with space (" ") also will be counted as a true value.

  3. If we declare a variable that is not defined yet or, declare a variable as Null / NAN will be counted as a false value.

  4. If we declare a variable whose value is false will count as a falsy value

  5. But if we declare an array with no element or an object with no property it will count as a true value and also the condition will be true.

Example1:

const age = 0;

if ( age ) {

console.log (" condition is true ");

}

else {

console.log( " condition is false " ) ;

}

Output:

condition is false

Example2:

const age = 10;

if ( age ) {

console.log (" condition is true ");

}

else {

console.log( " condition is false " ) ;

}

Output:

condition is true

What is the difference between Null and Undefined?

Different ways you will get undefined

Basically, Undefined means a variable that hasn't define yet or, you forget to define its value. There are many different ways when you will get undefined.

1 Suppose we have declared a variable ** let name** but forget to declare its value. When trying to print the name in the console we will see undefined as output.

let name;

console.log( name );

output:

undefined

2 Suppose, we have declared a function with needed parameters and done some calculation but didn't declare what will the function return when we will call it. In this case the only result we will able to see is undefined.

function add (number1, number2){

const sum = number1 + number2 ;

console.log( sum );

}

const result = add (5, 8) ;

console.log( result );

output:

undefined

3 Suppose, we have declared a function with needed parameters but when we are calling the function if the needed amount of parameters isn't defined we will get ** Undefined** as output.

4 Suppose, we have declared an object with some property. But, when we are trying to read some of its properties which is not belong to this object we will see undefined as output.

const person = { name: "John", age: 23, phone: "12345" };

console.log( person. salary );

output:

undefined

5 Suppose you have declared an array with three elements. But when you are trying to read it, you want to read or, get access to an element which don't exist in the array you will see undefined.

let names = [ "A", "B", "C" ]

console.log ( names.[5]);

output

undefined

Null means empty. We can't see null as an output till we didn't initially null as a value.

Uses Of Double And Tripple Equal

When we are trying to do a comparison between two variable by using double equal ( == ) it never checks the value type and the condition become true as a result. Suppose,

const first = 2; /* an integer /

const second = "2"; / a string */

if ( first == second ){

console.log ( " Your condition is true " );

}

else {

console.log (" Your condition is false " );

}

output

Your condition is true

From the above code, we can say that when we use double equal to compare values it didn't define the value type it only defines the values to perform the comparison.

But when it comes to triple equal ( === ) it always defines the values as well as the value type.

In the case of triple equal

const first = 2; /* an integer /

const second = "2"; / a string */

if ( first === second ) {

console.log ( " Your condition is true " );

}

else {

console.log (" Your condition is false " ); }

output

Your condition is false

What Is Scope In JavaScript?

  • Suppose we have declared a variable inside a function. Then the scope of that variable will be only inside the function. The variable becomes unable to use on the outside of that specific function.

  • If we declared a variable globally then its scope will be everywhere that's mean it will be accessible or, useable everywhere.

  • If we declared variable inside curly braces { } it will not accessible from the outside of those braces. But if the variable data type is Var then it will be accessible on the outside of those braces.

What Is Closure In JavaScript?

In the case of JavaScript, we can create an area that calls closure. If we call a function inside of another function then the called function makes a closed environment which is called Closure. When the called function accesses a variable from its parent function and returns something by using that variable every time the second function will store a value depending on how many times it calls.

What Is Window In JavaScript?

JavaScript has some properties like console, and document that we use, are belong to the window. That's why there is no need of using. to access them. A window is a javascript execution environment. Sometimes it calls Global Scope and it can be accessed by anyone from anywhere.

This keyword in JavaScript

In JavaScript, the keyword identifies whichever method or, function is running they are running by which context. If we use this keyword inside a class or a method then this keyword will represent the object of that class or method.

Explain What Is Async In JavaScript?

If you use the keyword async before a function basically, it will turn into a promise that is, access the data which could be possible or not. But we have to access this promise using then.

async function hello ( name ) {

return "Hello" + name ;

}

const greeting = hello ("Moon");

greeting.then( response => console.log (response));

Await

We can use await when you have done using ** async** in the previous section. Using await before a variable means waiting until you can't get the correct response.

Explain setTimeout In Your Words.

We know JavaScript always works synchronously. That means it executes its code by maintaining a serial. But we can break this synchronization using setTimeout. When we will use setTimeout to call a function it will execute after completing another execution.

function count () {

console.log(222) ;

}

console.log(111) ;

setTimeout ( count );

console.log( 444);

output

111

444

222