Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 112 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,128 +1,156 @@
# [JavaScript Interview Questions & Answers](https://github.com/ashish-pro/JavaScript-Interview)
# Click ⭐if you like the project and follow [@ashish-pro](https://github.com/ashish-pro)
# [JavaScript Interview Questions & Answers](https://github.com/ashish-pro/JavaScript-Interview)

# Contributers -
# Click ⭐if you like the project and follow [@ashish-pro](https://github.com/ashish-pro)

# Contributers -

# List of Questions
|No.| Questions |
|--|--|
| 1 | [what are the possible ways to create the object in JS?](#1-what-are-the-possible-ways-to-create-object-in-js)|
|2| What is the difference between == and === operators? |
| 3 | What do you mean by arrow function? |
| 4 | What is Rest Operators? |
| 5 | What is Spread Operators? |
|6 | Explain Closures with examples.|
|7 | What do mean by Hoisting?|

| No. | Questions |
| --- | -------------------------------------------------------------------------------------------------------------- |
| 1 | [what are the possible ways to create the object in JS?](#1-what-are-the-possible-ways-to-create-object-in-js) |
| 2 | [What is the difference between == and === operators?](#2-what-is-the-difference-between--and--operators) |
| 3 | [What do you mean by arrow function?](#3-what-do-you-mean-by-arrow-function) |
| 4 | What is Rest Operators? |
| 5 | What is Spread Operators? |
| 6 | Explain Closures with examples. |
| 7 | What do mean by Hoisting? |

# Solutions

# 1. what are the possible ways to create object in JS?

There are many ways to create objects in javascript as below
There are many ways to create objects in javascript as below

1. **Object constructor:**
1. **Object constructor:**

The simplest way to create an empty object is using the Object constructor. Currently this approach is not recommended.
The simplest way to create an empty object is using the Object constructor. Currently this approach is not recommended.

```javascript
var object = new Object();
```
```javascript
var object = new Object();
```

The `Object()` is a built-in constructor function so "new" keyword is not required. the above can be written as:
The `Object()` is a built-in constructor function so "new" keyword is not required. the above can be written as:

```javascript
var object = Object();
```
```javascript
var object = Object();
```

2. **Object's create method:**
2. **Object's create method:**

The create method of Object creates a new object by passing the prototype object as a parameter
The create method of Object creates a new object by passing the prototype object as a parameter

```javascript
var object = Object.create(null);
```
```javascript
var object = Object.create(null);
```

3. **Object literal syntax:**
3. **Object literal syntax:**

The object literal syntax (or object initializer), is a comma-separated set of name-value pairs wrapped in curly braces.
The object literal syntax (or object initializer), is a comma-separated set of name-value pairs wrapped in curly braces.

```javascript
var object = {
name: "Sudheer",
age: 34
};
```javascript
var object = {
name: "Sudheer",
age: 34
};

Object literal property values can be of any data type, including array, function, and nested object.
```
Object literal property values can be of any data type, including array, function, and nested object.
```

**Note:** This is an easiest way to create an object
**Note:** This is an easiest way to create an object

4. **Function constructor:**
4. **Function constructor:**

Create any function and apply the new operator to create object instances,
Create any function and apply the new operator to create object instances,

```javascript
function Person(name) {
this.name = name;
this.age = 21;
}
var object = new Person("Sudheer");
```
```javascript
function Person(name) {
this.name = name;
this.age = 21;
}
var object = new Person("Sudheer");
```

5. **Function constructor with prototype:**
5. **Function constructor with prototype:**

This is similar to function constructor but it uses prototype for their properties and methods,
This is similar to function constructor but it uses prototype for their properties and methods,

```javascript
function Person() {}
Person.prototype.name = "Sudheer";
var object = new Person();
```
```javascript
function Person() {}
Person.prototype.name = "Sudheer";
var object = new Person();
```

This is equivalent to an instance created with an object create method with a function prototype and then call that function with an instance and parameters as arguments.
This is equivalent to an instance created with an object create method with a function prototype and then call that function with an instance and parameters as arguments.

```javascript
function func() {}
```javascript
function func() {}

new func(x, y, z);
```
new func(x, y, z);
```

**(OR)**
**(OR)**

```javascript
// Create a new instance using function prototype.
var newInstance = Object.create(func.prototype)
```javascript
// Create a new instance using function prototype.
var newInstance = Object.create(func.prototype)

// Call the function
var result = func.call(newInstance, x, y, z),
// Call the function
var result = func.call(newInstance, x, y, z),

// If the result is a non-null object then use it otherwise just use the new instance.
console.log(result && typeof result === 'object' ? result : newInstance);
```
// If the result is a non-null object then use it otherwise just use the new instance.
console.log(result && typeof result === 'object' ? result : newInstance);
```

6. **ES6 Class syntax:**
6. **ES6 Class syntax:**

ES6 introduces class feature to create the objects
ES6 introduces class feature to create the objects

```javascript
class Person {
constructor(name) {
this.name = name;
}
```javascript
class Person {
constructor(name) {
this.name = name;
}
}

var object = new Person("Sudheer");
```

7. **Singleton pattern:**

A Singleton is an object which can only be instantiated one time. Repeated calls to its constructor return the same instance and this way one can ensure that they don't accidentally create multiple instances.

```javascript
var object = new (function () {
this.name = "Sudheer";
})();
```

**[⬆ Back to Top](#list-of-questions)**

# 2. What is the difference between == and === operators?

**Double equal:** The double equal(‘==’) operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison.

**Triple equal:** The triple equal(‘===’) operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it will return false.

In this example, we will check abstract and strict quality. One will return true between a string 9 and number 9. Because there is no type comparison, in the case of ‘===’ it will return false

var object = new Person("Sudheer");
```
```javascript
// In R.H.S. string "9" is converted into
// number 9, hence returns true.
console.log(9 == "9");

7. **Singleton pattern:**
// Here no type of conversion takes place,
// hence returns false
console.log(9 === "9");
```

A Singleton is an object which can only be instantiated one time. Repeated calls to its constructor return the same instance and this way one can ensure that they don't accidentally create multiple instances.
# 3. What do you mean by arrow function?

```javascript
var object = new (function () {
this.name = "Sudheer";
})();
```
Arrow functions are anonymous functions i.e. functions without a name and are not bound by an identifier.

**[⬆ Back to Top](#list-of-questions)**
```javascript
// Syntax- Arrow funtion to add two numbers
const add = (x, y) => x + y;
```