Basic JS Concepts
Tags: javascript, programming
Published: 2022-03-05 09:32:23

**How does JavaScript work? 
  or What is the JavaScript Event Loop?**
 
Ans: In JavaScript, the event loop is a process that waits for the Call Stack to be clear before pushing callbacks from the Task Queue to the Call Stack. Once the Stack is clear, the event loop triggers and checks the Task Queue for available callbacks and If there are any, it pushes it to the Call Stack, waits for the Call Stack to be clear again, and repeats the same process

  **Or Though JavaScript is single-threaded, how does it handle concurrent work?** 
  **Or Is JavaScript Single-threaded or multi-threaded?**

Ans: JavaScript is a single-threaded language. Because the language specification does not allow the programmer to write code so that the interpreter can run parts of it in parallel in multiple threads or processes.
  Or Is JavaScript Synchronous or asynchronous?
Ans: JavaScript is a client-side and server-side scripting language inserted into HTML pages and is understood by web browsers. JavaScript is also an Object-based Programming language.

**How does JavaScript code is executed in Browser?**

Ans: Mostly every web browsers nowadays have their own JavaScript engines. So, it is the JavaScript engine that understands the code and runs it.

In this case, we have used a chrome browser to run our program that has the "V8" JavaScript engine, which is also used for creating the Node.js. As we already know, JavaScript is an interpreted language that means it gets executed in line by line manner or which means the JavaScript engine converts the Js code line by line and runs in the same manner instead of converting the whole program once.

**What are the differences between “==” and “===” ?**

Ans: Both are comparison operators and “==” is used to compare only values whereas “===” is used to compare both values and types.

4. **What is a callback function?**

Ans: In JavaScript the definition of  callback is a plain JavaScript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘call back‘.

**When will you return something from a function?** 
   **Or How will you return more than one value from a function?**

Ans: The return statement is used to return a particular value from the function to the function caller. So we can say that the function will stop executing when the return statement is called and the return statement should be the last statement in a function because the code after the return statement will be unreachable.

We can return primitive values (such as Boolean, number, string, etc.) and Object types (such as functions, objects, arrays, etc.) by using the return statement.

We can also return multiple values using the return statement. It cannot be done directly so that we have to use an Array or Object to return multiple values from a function.
 
**Tell me about bind, call and apply.
   Or How many arguments does call apply bind take?**

Ans: 
Call method invokes the function and allows you to pass in arguments one by one and Apply method invokes the function and allows you to pass in arguments as an array whereas Bind method returns a new function, allowing you to pass in a this array and any number of arguments.

**What is a Closure in JavaScript? How does it work?**

Ans: A closure can be defined as a JavaScript feature in which the inner function has access to the outer function variable. The closure has three scope chains listed as follows:

- Access to its own scope.
- Access to the variables of the outer function.
- Access to the global variables.

It gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created. To use a closure, simply define a function inside another function and expose it.

**What does the “this” keyword indicate in JavaScript?**

Ans: In JavaScript this keyword refers to the object it belongs to. It has different values depending on where it is used. In a method, this refers to the owner object and in a function, this refers to the global object.

**What is Event bubbling in js?**

Ans: Event bubble is a type of event propagation where the event is first triggered on the most bottom target element and then continuously triggered on the ancestor of the target element in the same nesting hierarchy until it reaches the external DOM element or document object.
 
 **Or How does event delegate work in JS?**

Ans: Event delegate works such a way if we have a lot of elements handled in a similar way, then instead of assigning a handler to each of them – we put a single handler on their common ancestor.

**Explain hoisting in JavaScript.**

Ans: Housing is the default behavior of javascript. Here declaration of functions and variables are moved on top of the scope.

**What is a recursive function function?**

Ans: Recursion is a process of calling itself until it arrives at a result. So we can say that, when a function that calls itself is called a recursive function and recursive function must have a condition to stop calling itself. If we do not stop it, the function is called indefinitely.
 
**Difference between undefined and null.**

Ans: Null is an object that means it is an assignment value whereas undefined is a type that means a variable is declared, but no value has been assigned a value.
 
**NULL example,**
`var demo = null;
alert(demo); //shows null
alert(typeof demo); //shows object`

**Undefined example,**

`var demo;
alert(demo); //shows undefined
alert(typeof demo); //shows undefined`
 
 
**What are the different data types in JavaScript?**
Ans: JavaScript has 7 types of data types. These are:
Number
String
Boolean
Undefined
Null
Object
Symbol(From ES6)
 
   **Or Primitive data type and non-primitive data type**

Ans: Primitives are known as being immutable data types because there is no way to change a primitive value once it gets created. Primitives are compared by value.

Non-primitive values are mutable data types. The value of an object can be changed after it gets created. Objects are not compared by value.

**What is DOM?**

Ans: JavaScript can access all the elements in a web page using the Document Object Model (DOM). The web browser creates a DOM of the webpage when the page is loaded.

**Is JavaScript a static type or a dynamic type?**
     **Or How will you know the type of a JavaScript variable?**

Ans: JavaScript is a dynamically typed language. In a dynamically typed language, the type of a variable is checked during run-time in contrast to statically typed language, where the type of a variable is checked during compile-time.

**What are the scopes of a variable in JavaScript?**

Ans: The scope of a variable means it is the region of a program in which it is defined. JavaScript variable will have only two scopes.

• **Global Variables** − A global variable which has global scope and it is visible everywhere in your JavaScript code.

• **Local Variables** − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

**What are the variable naming conventions in JavaScript?**

For naming variables in JavaScript following rules are to be followed:

Should not use any of the JavaScript reserved keyword as variable name. For example, break or boolean variable names are not valid.
Should not start with a numeral (0-9) for making JavaScript variable names. They must begin with a letter or the underscore character. For example, 123name is an invalid variable name but _123name or name123 is a valid one.

JavaScript variable names are case sensitive. For example, Test and test are two different variables.
 
 
**In how many ways can you create an array in JS?**

There are three different ways of creating an array in JavaScript, namely:

**By creating instance of an array:**
var someArray = new Array();

**By using an array constructor:**
var someArray = new Array(‘value1’, ‘value2’,…, ‘valueN’);

**By using an array literal:**
var someArray = [value1, value2,…., valueN];

**What are global variables? How are these variables declared?**

Ans: Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using var for use only within that function’s scope. If you declare a variable without using var, even if it’s inside a function, it will still be seen as global.

**What is the use of the Push method in JavaScript?**
Ans: The push method is used to add or append one or more elements to an Array end. Using this method, we can append multiple elements by passing multiple arguments.

**How are event handlers utilized in JavaScript?**

Ans: Events are the actions that result from activities, such as clicking a link or filling a form by the user. An event handler is required to manage the proper execution of all these events. Event handlers are an extra attribute of the object. This attribute includes the event’s name and the action taken if the event takes place.

**What is the unshift method in JavaScript?**

Ans: The method of unshift is like the push method, which works at the beginning of the array and this method is used to prepend one or more elements to the beginning of the array.

**What is Implicit Type Coercion in javascript?**

Ans: Implicit type coercion in javascript is automatic conversion of value from one data type to another. It takes place when the operands of an expression are of different data types.

**What is NaN property in JavaScript?**

Ans: In JavaScript, NaN property means Not a Number and It represents a value that is not a valid number. It can be used to check whether a number entered is a valid number or not a number.

**What is the typeof operator?**
Ans: JavaScript typeof operator is used to find the type of a JavaScript variable. It returns the type of a variable or an expression.
 
 
 
-------------------**Notes for Hoisting**-------------------------

****Note - Variable initializations are not hoisted, only variable declarations are hoisted**

****Note - To avoid hoisting, you can run javascript in strict mode by using “use strict” on top of the code**