JavaScript Assginment
Tags: javascript, webdev, beginners, programming
Published: 2022-05-10 12:14:44


1. **what are the different data types present in javaScript?**

ans

> 
.Primitive values
All types except objects define immutable values (that is, values which can't be changed). For example (and unlike in C), Strings are immutable. We refer to values of these types as "primitive values".

> 
.Boolean type;
Boolean represents a logical entity and can have two values: true and false.

> 
.Null type;
The Null type has exactly one value.

> 
.Underfined type;
A variable that has not been assigned a value has the value undefined.
.Number type;ECMAScript has two built-in numeric types,number and bigint .

> 
.BigInt type;The BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit for Numbers.

> 
.Objects;In computer science, an object is a value in memory which is possibly referenced by an identifier.

2.**Explain Hoisting in JavaScript**
ans
JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.
Hoisting allows functions to be safely used in code before they are declared.
Variable and class declarations are also hoisted, so they too can be referenced before they are declared. Note that doing so can lead to unexpected errors, and is not generally recommended.

3.**The Difference Between == and ===**
ans

> 
== converts the variable values to the same type before performing comparison. This is called type coercion.

> 
=== does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

4.**Explain Implicit Type Coercion JavaScript**
ans
coercion is the process of conversion of data from one type to another,
There are cases where JavaScript implicitly does coercion and we aren’t aware of it.
Coercion applies to every data type present in the JavaScript world. we’re explicitly converting a string to a number by passing the string to the inbuilt Number() function.
There are two types of coercion in JavaScript:
Implicit Coercion: Type conversion is done implicitly by JavaScript.
Explicit Coercion: Type conversion is done explicitly in code using the inbuilt functions like Number(), String(), Boolean(), etc.

5.**Is javascript a statically typed or a dynamically typed language?**
ans
JavaScript is called a dynamic language because it doesn't just have a few dynamic aspects, pretty much everything is dynamic.
All variables are dynamic (both in type and existance), and even the code is dynamic. You can create new variables at runtime, and the type of variables is determined at runtime.

6.**What is NaN property in JavaScript?**
ans
In JavaScript, NaN stands for Not a Number. It represents a value which is not a valid number. It can be used to check whether a number entered is a valid number or not a number. To assign a variable to NaN value, we can use one of the two following ways.

7.**Explain passed by value and passed by reference**
ans

- "Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9.

- "Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9

8.**What is an Immediately Invoked Function in javascript?**
ans
An Immediately-invoked Function Expression is a way to execute functions immediately, as soon as they are created. IIFEs are very useful because they don't pollute the global object, and they are a simple way to isolate variables declarations.

JavaScript Function Invocation is used to executes the function code and it is common to use the term “call a function” instead of “invoke a function”. The code inside a function is executed when the function is invoked

9.**Explain Higher Order Functions in javascript**
ans
In Javascript, functions can be assigned to variables in the same way that strings or arrays can. They can be passed into other functions as parameters or returned from them as well.
A “higher-order function” is a function that accepts functions as parameters and/or returns a function.

10.**Explain “this” keyword**
ans
The this keyword refers to the current object in a method or constructor.
The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

11.**Explain call(), apply() and, bind() methods.**
ans

- bind().
The bind method creates a new function and sets the this keyword to the specified object,We can also pass extra arguments to the bind method. The general syntax for this is function.bind(this, arg1, arg2, ...).

- call().
The call method sets the this inside the function and immediately executes that function.
The difference between call() and bind() is that the call() sets the this keyword and executes the function immediately and it does not create a new copy of the function, while the bind() creates a copy of that function and sets the this keyword.

- apply().
The apply() method is similar to call(). The difference is that the apply() method accepts an array of arguments instead of comma separated values.

12.**What is Currying in javascript?**
ans
Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline.
In other words, when a function, instead of taking all arguments at one time, takes the first one and return a new function that takes the second one and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.

13.**Explain Scope and Scope Chain in javascript.**
ans
Scope chains establish the scope for a given function. Each function defined has its own nested scope, and any function defined within another function has a local scope which is linked to the outer function — this link is called the chain.

Scope is the way of accessing variables, functions, and objects in some particular part of your code during runtime.
With scope, we provide some level of security to our code. We have access to the stuff we need at a time.
There are two kinds of scope — Global scope and Local scope.
> 
Global Scope
Variables defined outside a function are in the global scope. Also, There is only one Global scope throughout a JavaScript document.Once you’ve declared a global variable, you can use that variable anywhere in your code, even in function
> 
Local Scope
Variables defined inside a function are in local scope.And they have a different scope for every call of that function. Also, Each function when invoked creates a new scope. So there is a Function scope also.



14.**Explain Closures in JavaScript.**
ans
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. In JavaScript, closures are created every time a function is created, at function creation time.

15.**What are object prototypes?**
ans
JavaScript is a prototype based language, so, whenever we create a function using JavaScript, JavaScript engine adds a prototype property inside a function, Prototype property is basically an object (also known as Prototype object), where we can attach methods and properties in a prototype object, which enables all the other objects to inherit these methods and properties.

16.**What are callbacks?**
ans
A callback is a function passed as an argument to another function
This technique allows a function to call another function
A callback function can run after another function has finished.

17.**What is memoization ?**
ans
 What is memoization? “Memoization is an optimization technique where expensive function calls are cached such that the result can be immediately returned the next time the function is called with the same arguments”

18.__**What is recursion in a programming language?**
ans
In computer science, recursion is a programming technique using function or algorithm that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first.

19.**What is the use of a constructor function in javascript?
**
ans
A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword.
The purpose of a constructor is to create a new object and set values for any existing object properties.

20.**What is DOM?**
ans
DOM stands for Document Object Model. It is a programming interface that allows us to create, change, or remove elements from the document. We can also add events to these elements to make our page more dynamic.
The DOM views an HTML document as a tree of nodes. A node represents an HTML element.