10 Javascript Very Basic Interview Questions You Should Know
Tags: javascript, webdev, programming
Published: 2021-05-08 15:46:17

> This article is especially for beginners who are looking for Job as a Javascript Developer. I have searched a lot of Javascript interview questions and these 10 seem most important to me. Let's dig into this a bit.

## 1. What is Javascript?
Javascript is a programming language that is used for Web Development. JavaScript runs on the client-side of the web.

According to MDN JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it's used in many non-browser environments as well. It is a prototype-based, multi-paradigm scripting language that is dynamic, and supports object-oriented, imperative, and functional programming styles.

## 2. What is DOM
DOM stands for Document Object Model. When a web page is loaded the browser creates a DOM using the HTML and CSS file. The DOM is represented by nodes and elements. You can manipulate DOM using javascript. It's a tree-like structure.

## 3. How JS Code is Executed
It's a bit large question to answer in an Interview. But we can say it briefly. Javascript runs on the browser. There's a javascript engine for almost every browser. V8 is the most popular of them. Chrome uses the V8 engine. On the other hand, Firefox uses the Spider-Monkey engine.

## 4. Difference between == and ===
If I say this shortly, == only checks if both values are the same or not. It doesn't check the type of these values. Check out the code below:
```
if(2=="2"){
 console.log("true")
} else {
console.log("false")
}
```
The above code will log true. Because it treats both 2 and "2" as equal as it doesn't check the types.

On the contrary === checks both type and quality. For instance:
```
if(2==="2"){
 console.log("true")
} else {
console.log("false")
}
```
This will log false. Because 2 and "2" are equal by values but they are of different types.

## 5. Null vs Undefined
Generally, null means empty and non-existent value while undefined means something that is declared but not defined yet. Though you can explicitly set undefined to a variable too.
```
var n;
console.log(typeof(n)); // undefined

var n = null;
console.log(typeof(n)); // object
```
Interestingly null is an object type in JS.

## 6. Var vs Let vs Const 
Before ES6 var was the only way to declare a variable. But now we have more options. 


There is a term as scope. Scope means where these variables are available for use. var declarations are globally scoped or function/locally scoped.

Var can be hoisted which we will discuss a few seconds later. But now let is more preferable for variable declarations. You can use const when there is no need to change a variable later in the code. To get the difference between this you can read the following article which I found pretty useful.

## 7. Hoisting
In javascript, a variable can be used before it has been declared. The concept of variable and function declarations to physically moved to the top of your code is called hoisting.
```
console.log(num); // Returns undefined, as only declaration was hoisted, no initialization has happened at this stage
var num; // Declaration
num = 6; 
```
So are let and const variables not hoisted? The answer is a bit more complicated than that. All declarations (function, var, let, const, and class) are hoisted in JavaScript, while the var declarations are initialized with undefined, but let and const declarations remain uninitialized.

## 8. Global vs Local Scope
In javascript, scopes are divided in two ways. Local and global.
Variables declared in the function are called local scope. That variable can't be accessed outside of the function. On the contrary, variables declared outside of a function are called global scope. it can be accessed inside the function.
```
var genre= "superhero" //global scope
// code here can't use superhero but genre
function myFunction() {
  var superhero = "Batman"; // local scope

  // code here CAN use superhero and genre

}
```

## 9. Closure
A closure gives us access to an outer function’s scope from an inner function. It can be created by returning another function from a function. It creates a close environment for each instance. For example:
```
function sum(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = sum(5);
var add10 = sum(10);

console.log(add5(6));  // 11
console.log(add10(6)); // 16
```
Here add5 and add10 are both closures. They share the same definition but store different environments.  To know more about closures you can follow the link below:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

## 10. Callback Function
According to MDN, A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. For instance
```
function greeting(name) {
  console.log('Hello ' + name);
}

function greetEmployee(name,callback) {
  
  callback(name);
}

greetEmployee("Dwight",greeting);
```
Here the greeting function has been used inside the greetEmployee function. This is what we call a callback function. Follow [this](https://www.w3schools.com/js/js_callback.asp) to grasp more about js callback.

Thanks for reading this. Hope this will help you.

Stay in touch
Happy coding