You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| 42 |[Difference between push() and unshift()](#42-difference-between-push-and-unshift)|
51
51
| 43 |[Difference between pop() and shift()](#43-difference-between-pop-and-shift)|
52
+
| 44 |[Different ways to access DOM elements in js](#44-different-ways-to-access-dom-elements-in-js)|
53
+
| 45 |[What are promises](#45-what-are-promises)|
54
+
| 46 |[What is a prototype](#46-what-is-a-prototype)|
55
+
| 47 |[What is a callback hell](#47-what-is-a-callback-hell)|
56
+
| 48 |[What is an event loop](#48-what-is-an-event-loop)|
57
+
| 49 |[ES6 and its features](#49-es6-and-its-features)|
58
+
| 50 |[Difference between function and method](#50-difference-between-function-and-method)|
52
59
53
60
### 1. What is JavaScript
54
61
* JavaScript is a scripting language used to create dynamic and interactive websites. It is supported by all major web browsers.
@@ -664,6 +671,107 @@ let newArr = arr.shift();
664
671
console.log(newArr); // output ========> 1
665
672
console.log(arr); // output ========> [2,3,4]
666
673
```
674
+
675
+
### 44. Different ways to access DOM elements in js
676
+
*getElementById* - This method is used to get an element by its ID.
677
+
678
+
*getElementsByClassName* - This method is used to get a collection of elements by their class name.
679
+
680
+
*getElementsByTagName* - This method is used to get a collection of elements by their tag name.
681
+
682
+
*querySelector* - This method is used to get the first element that matches a specified CSS selector.
683
+
684
+
*querySelectorAll* - This method is used to get a collection of elements that match a specified CSS selector.
685
+
686
+
### 45. What are promises
687
+
In JavaScript, promises are used to handle asynchronous operations. The code does not directly or immediately return a value. Instead, it returns a promise that, it will eventually get resolved or rejected.
688
+
A promise can have three possible states:
689
+
690
+
*Pending* - The initial state, before the promise is resolved or rejected.
691
+
692
+
*Resolved* - When a promise has been successfully completed and a value is returned.
693
+
694
+
*Rejected* - When a promise has been failed and an error is returned.
695
+
696
+
```js
697
+
let promise =newPromise((resolve, reject) => {
698
+
constresult=AsynchronousTaskFunction();
699
+
if (result) {
700
+
resolve(result);
701
+
} else {
702
+
reject(newError('Operation failed'));
703
+
}
704
+
});
705
+
706
+
promise.then(result=> {
707
+
console.log(result, "It is resolved");
708
+
}).catch(error=> {
709
+
console.error(error, "It is rejected");
710
+
});
711
+
```
712
+
713
+
### 46. What is a prototype
714
+
Every object in JavaScript has a built-in property, which is called its prototype. All JavaScript objects inherit properties and methods from a prototype.
715
+
716
+
It allows us to add properties and methods to all instances of a given object type.
717
+
```js
718
+
719
+
functionPerson(name, age) {
720
+
this.name= name;
721
+
this.age= age;
722
+
}
723
+
Person.prototype.city="Indore"
724
+
var person1 =newPerson('John', 30);
725
+
console.log(person1);
726
+
```
727
+
728
+
### 47. What is a callback hell
729
+
Callback hell is a situation in which callback functions are nested inside each other at several levels, making the code difficult to read, write, and maintain.
730
+
```js
731
+
asyncFunc1(function(response1) {
732
+
asyncFunc2(response1, function(response2) {
733
+
asyncFunc3(response2, function(response3) {
734
+
asyncFunc4(response3, function(response4) {
735
+
asyncFunc5(response4, function(response5) {
736
+
// ... and so on
737
+
});
738
+
});
739
+
});
740
+
});
741
+
});
742
+
```
743
+
744
+
### 48. What is an event loop
745
+
The event loop is a key mechanism in JavaScript that provides an illusion of being multithreaded despite being single-threaded, allowing for non-blocking, asynchronous processing of events and callbacks.It monitors both the callback queue and the call stack.
746
+
747
+
If the call stack is not empty, the event loop waits until it is empty
748
+
749
+
If the call stack is empty it places the next function from the callback queue to the call stack
750
+
751
+
752
+
### 49. ES6 and its features
753
+
ES6 stands for ECMAScript 6, also known as ECMAScript 2015. It is the sixth major version of the ECMAScript language specification for JavaScript. Below are some of the significant features of ES6
754
+
755
+
*Arrow functions, template literals, block-scoped variables (let and const), default function parameters, array and object destructing, promises, rest and spread operators, classes.*
756
+
757
+
### 50. Difference between function and method
758
+
**method** - A method is a function associated with an object.
759
+
```js
760
+
let obj = {
761
+
name:"Surbhi",
762
+
greet:function(){
763
+
return`Hi ${this.name}`
764
+
}
765
+
}
766
+
console.log(obj.greet());
767
+
```
768
+
**function** - A function is a self-contained block of code that can be defined and called independently of any object.
0 commit comments