DEV Community

loading...

10 interesting and important JavaScript questions

Sayem Mohammad Ismail
Full-stack web developer
・5 min read

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

Answer:

The double equal does not check for the type of the value. It only checks if the value is the same regardless of their types.

const first = 10; // number
const second = ‘10’; // string
if (first == second) {
    console.log(‘condition true’); //this will be logged
} else {
    console.log(‘condition false’);
}
Enter fullscreen mode Exit fullscreen mode

Here, the condition becomes true because == says 10 and '10' are equal, though one of them is a number and the other one is a string. Their types are ignored by ==.

Another example,

const first = 1; //number
const second = true; //boolean 

if (first == second) {
    console.log(‘condition true’); //this will be logged
} else {
    console.log(‘condition false’);
}
Enter fullscreen mode Exit fullscreen mode

Here, with == the Boolean true is taken as value 1 and if there was the Boolean false it would have taken a value 0 for false. Then the value 1 for Boolean true is compared with the number 1 and states the condition to be true. Once again the types are ignored by ==.

However, the === checks for the types as well. It would have stated the conditions of both of the examples here to be false and logged the else statement 'condition false'.

What is Window ?

Answer:

The JavaScript Window object is the field of JavaScript. It is the playground for JavaScript to play.

JavaScript Window is an Object that contains everything global. All global variables, functions, objects are part of the window. Global variables are properties of the Window object and global functions are methods of it.
If you check in the console, document === window.document it will display true.

We use console.log very often. console === window.console will say true.

Items in the window object can be accessed directly in the javaScript.

What is Scope and local and global variables?

Answer:

Scope can be said to be the limit or range of variables. A function has its scope within that function only and the whole JavaScript project has the Global Scope.

A variable gets the associated scope where it is defined. If it is defined within a function then it has the local scope within that function and becomes a local variable. The variable cannot be accessed from outside the function.

However, if a variable is defined outside of any function then it gets the global scope and becomes a global variable. A global variable can be accessed from anywhere in the JavaScript project and obviously from within any function.

What is the ‘this’ keyword ?

Answer:

The JavaScript keyword this refers to the context of the current running functionality. For example, if an object myObj has a property name and a method getName(), and the key word this is used within the method then this will refer to the context myObj.

const myObj = {
    name: ‘Tom Cruise’,
    getName: function() {
        return console.log(this.name);
        }
};

myObj.getName();  // ‘Tom Cruise’ will be logged in the console.
Enter fullscreen mode Exit fullscreen mode

What is JavaScript Event loop?

Answer:

The Event loop is a loop of events. It has two concepts: call stack and event queue.
JavaScript is a single threaded programming language that can do one task at a time. If multiple tasks arrive, it simply adds all of them to a queue which is called an event queue. The event queue is just any real life queue, the event comes first and gets to be done first. Once the thread is done with the current event it picks up the next event from the queue and starts performing that event. The thread continues this way until the event queue is empty.

The thread works on each event with a call stack. When an event calls a function, the thread then stacks that function up on the event and any other function called inside the previous function also gets stacked up. Then the thread starts performing from the top of the stack eliminating each function down the line to get to the bottom of the stack and makes the call stack empty.

How does JavaScript code execute?

Answer:

The web browser Google chrome uses an engine V8 to run the JavaScript on the browser. V8 engine takes the JavaScript source code and reads it. It then performs the just in time compilation on the JavaScript code and makes some optimizations. Finally, V8 engine provides the result/output.

How does a recursive function work?

Answer:

In order to perform a task, a recursive function calls itself over and over again with updated inputs until it reaches a stopping point.

For example, let’s say, we need to find the factorial of 5 or 5! Writing a recursive function findFactorial(),

function findFactorial (number) {
    if (number ===1) {
    return number;
    } else {
    return number * findFactorial(number - 1);
    }
} 

const result = findFactorial(5); 
console.log(result); //result is 120
Enter fullscreen mode Exit fullscreen mode

What is DOM?

Answer:

DOM stands for Document Object Model. To show a HTML file in a web page, the browser converts the HTML file into a JavaScript object. This JavaScript object is known as DOM. The whole HTML file is contained in it.

What is a callback function?

Answer:

A function that is passed as an argument in another function is a call back function.

function greetFriend(name){
    console.log(“Hello ”, name);
}  
function  doGreeting(name, handleGreeting){
    handleGreeting(name);
}

doGreeting(“Tom”, greetFriend); // “Hello Tom” 
Enter fullscreen mode Exit fullscreen mode

greetFriend is passed in as a call back function to the function doGreeting. Here, greetFriend is a named function that has a name to call it. The call back function can be anonymous as well, that is it won’t have a name.

doGreeting(“Tom”, function(name){
    console.log(“Hello”, name);
}) 
//”Hello Tom” will be logged here too.
Enter fullscreen mode Exit fullscreen mode

The call back here does not have a name and so, it is an anonymous call back function.

What is an API and what do GET and POST do?

Answer:

API stands for Application programming Interface. API is what facilitates communication or interaction between applications or usually between client side application and server/servers or system etc.

In simple terms, API is the medium that takes the request of the user and delivers it to the server or system, then takes the response of the system or server and delivers the response to the user.

For example, all our online activity is facilitated by APIs. If you sign up to a website, there is an API that takes your request and system stores your credentials to the database and allows you access, then API delivers your access to you and you are signed in. There are many more such examples.

The GET request characterizes an API to perform the job of delivering the response of the system to the user upon user request.
And the POST request characterizes an API to send or store data or user response to the system or server or database.

Discussion (2)

Collapse
ayeprahman profile image
Arif Rahman

A nice understanding of interview question =)

Collapse
smismail profile image
Sayem Mohammad Ismail Author

Thank you, brother.