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
Copy file name to clipboardExpand all lines: README.md
+28-21Lines changed: 28 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -853,7 +853,7 @@ In the book ["You Don't Know JS"](https://github.com/getify/You-Dont-Know-JS/tre
853
853
854
854
In simple terms, functions have access to variables that were in their scope at the time of their creation. This is what we call the function's lexical scope. A closure is a function that retains access to these variables even after the outer function has finished executing. This is like the function has a memory of its original environment.
855
855
856
-
```js
856
+
```js live
857
857
functionouterFunction() {
858
858
constouterVar='I am outside of innerFunction';
859
859
@@ -1822,7 +1822,7 @@ On the other hand, `WeakSet` only allows objects as elements, and these object e
1822
1822
1823
1823
Static class members (properties/methods) has a `static` keyword prepended. Such members cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
1824
1824
1825
-
```js
1825
+
```js live
1826
1826
classCar {
1827
1827
static noOfWheels =4;
1828
1828
staticcompare() {
@@ -1855,7 +1855,7 @@ Static members are useful under the following scenarios:
1855
1855
1856
1856
`Symbol`s in JavaScript are a new primitive data type introduced in ES6 (ECMAScript 2015). They are unique and immutable identifiers that is primarily for object property keys to avoid name collisions. These values can be created using `Symbol(...)` function, and each `Symbol` value is guaranteed to be unique, even if they have the same key/description. `Symbol` properties are not enumerable in `for...in` loops or `Object.keys()`, making them suitable for creating private/internal object state.
`Symbol`s in JavaScript are a new primitive data type introduced in ES6 (ECMAScript 2015). They are unique and immutable identifiers that is primarily for object property keys to avoid name collisions. These values can be created using `Symbol(...)` function, and each `Symbol` value is guaranteed to be unique, even if they have the same key/description. `Symbol` properties are not enumerable in `for...in` loops or `Object.keys()`, making them suitable for creating private/internal object state.
2414
2414
2415
-
```js
2415
+
```js live
2416
2416
let sym1 =Symbol();
2417
2417
let sym2 =Symbol('myKey');
2418
2418
@@ -2568,11 +2568,11 @@ var bar = function () {
2568
2568
2569
2569
Hoisting can lead to unexpected behavior in JavaScript because variable and function declarations are moved to the top of their containing scope during the compilation phase. This can result in `undefined` values for variables if they are used before their declaration and can cause confusion with function declarations and expressions. For example:
2570
2570
2571
-
```js
2571
+
```js live
2572
2572
console.log(a); // undefined
2573
2573
var a =5;
2574
2574
2575
-
console.log(b); // ReferenceError: b is not defined
2575
+
console.log(b); // ReferenceError: Cannot access 'b' before initialization
2576
2576
let b =10;
2577
2577
```
2578
2578
@@ -4135,7 +4135,7 @@ Things to note are:
4135
4135
4136
4136
The prototype chain is a mechanism in JavaScript that allows objects to inherit properties and methods from other objects. When you try to access a property on an object, JavaScript will first look for the property on the object itself. If it doesn't find it, it will look at the object's prototype, and then the prototype's prototype, and so on, until it either finds the property or reaches the end of the chain, which is `null`.
4137
4137
4138
-
```js
4138
+
```js live
4139
4139
functionPerson(name) {
4140
4140
this.name= name;
4141
4141
}
@@ -4401,7 +4401,7 @@ The main takeaway here is that `this` can be changed for a normal function, but
4401
4401
4402
4402
Static class members (properties/methods) has a `static` keyword prepended. Such members cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
4403
4403
4404
-
```js
4404
+
```js live
4405
4405
classCar {
4406
4406
static noOfWheels =4;
4407
4407
staticcompare() {
@@ -4436,7 +4436,7 @@ In the book ["You Don't Know JS"](https://github.com/getify/You-Dont-Know-JS/tre
4436
4436
4437
4437
In simple terms, functions have access to variables that were in their scope at the time of their creation. This is what we call the function's lexical scope. A closure is a function that retains access to these variables even after the outer function has finished executing. This is like the function has a memory of its original environment.
To get the query string values of the current page in JavaScript, you can use the `URLSearchParams` object. First, create a `URLSearchParams` instance with `window.location.search`, then use the `get` method to retrieve specific query parameters. For example:
This will give you the value of the query parameter named `key`.
5535
+
This will give you the value of the query parameter named `language`. If you look at the URL of this page, you can see that the `language` parameter is set to 'js'.
To create custom error objects in JavaScript, you can extend the built-in `Error` class. This allows you to add custom properties and methods to your error objects. Here's a quick example:
5870
5871
5871
-
```js
5872
+
```js live
5872
5873
classCustomErrorextendsError {
5873
5874
constructor(message) {
5874
5875
super(message);
@@ -5928,7 +5929,7 @@ try {
5928
5929
5929
5930
Currying is a technique in functional programming where a function that takes multiple arguments is transformed into a series of functions that each take a single argument. This allows for partial application of functions. For example, a function `f(a, b, c)` can be curried into `f(a)(b)(c)`. Here's a simple example in JavaScript:
@@ -6033,10 +6039,11 @@ Currying transforms a function with multiple arguments into a sequence of functi
6033
6039
6034
6040
`Set`s and `Map`s are built-in JavaScript objects that help manage collections of data. A `Set` is a collection of unique values, while a `Map` is a collection of key-value pairs where keys can be of any type. `Set`s are useful for storing unique items, and `Map`s are useful for associating values with keys.
6035
6041
6036
-
```js
6042
+
```js live
6037
6043
// Set example
6038
-
let mySet =newSet([1, 2, 3, 3]);
6039
-
mySet.add(4); // Set {1, 2, 3, 4}
6044
+
let mySet =newSet([1, 2, 3, 3]); // Set {1, 2, 3} (duplicate values are not added)
6045
+
mySet.add(4);
6046
+
console.log(mySet); // Set {1, 2, 3, 4}
6040
6047
6041
6048
// Map example
6042
6049
let myMap =newMap();
@@ -6149,7 +6156,7 @@ Both `Map` objects and plain objects in JavaScript can store key-value pairs, bu
6149
6156
6150
6157
`Set`s and `Map`s in JavaScript handle equality checks for objects based on reference equality, not deep equality. This means that two objects are considered equal only if they reference the same memory location. For example, if you add two different object literals with the same properties to a `Set`, they will be treated as distinct entries.
6151
6158
6152
-
```js
6159
+
```js live
6153
6160
constset=newSet();
6154
6161
constobj1= { a:1 };
6155
6162
constobj2= { a:1 };
@@ -6527,7 +6534,7 @@ Design patterns are reusable solutions to common problems in software design. Th
6527
6534
6528
6535
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is useful when exactly one object is needed to coordinate actions across the system. In JavaScript, this can be implemented using closures or ES6 classes.
6529
6536
6530
-
```js
6537
+
```js live
6531
6538
classSingleton {
6532
6539
constructor() {
6533
6540
if (!Singleton.instance) {
@@ -6632,7 +6639,7 @@ myModule.publicMethod(); // Logs: I am private
6632
6639
6633
6640
The Prototype pattern is a creational design pattern used to create new objects by copying an existing object, known as the prototype. This pattern is useful when the cost of creating a new object is more expensive than cloning an existing one. In JavaScript, this can be achieved using the `Object.create` method or by using the `prototype` property of a constructor function.
Copy file name to clipboardExpand all lines: questions/describe-the-difference-between-a-cookie-sessionstorage-and-localstorage/en-US.mdx
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ The CookieStore API is relatively new and may not be supported in all browsers (
100
100
-**Access**: Data is accessible within all tabs and windows of the same origin.
101
101
-**Security**: All JavaScript on the page have access to values within `localStorage`.
102
102
103
-
```js
103
+
```js live
104
104
// Set a value in localStorage.
105
105
localStorage.setItem('key', 'value');
106
106
@@ -123,7 +123,7 @@ localStorage.clear();
123
123
-**Access**: Data is only accessible within the current tab or window. Different tabs or windows with the same page will have different `sessionStorage` objects.
124
124
-**Security**: All JavaScript on the same page have access to values within `sessionStorage` for that page.
Copy file name to clipboardExpand all lines: questions/explain-the-concept-of-the-prototype-pattern/en-US.mdx
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: Explain the concept of the Prototype pattern
6
6
7
7
The Prototype pattern is a creational design pattern used to create new objects by copying an existing object, known as the prototype. This pattern is useful when the cost of creating a new object is more expensive than cloning an existing one. In JavaScript, this can be achieved using the `Object.create` method or by using the `prototype` property of a constructor function.
8
8
9
-
```js
9
+
```js live
10
10
constprototypeObject= {
11
11
greet() {
12
12
console.log('Hello, world!');
@@ -35,7 +35,7 @@ In JavaScript, the Prototype pattern can be implemented using the `Object.create
35
35
36
36
The `Object.create` method creates a new object with the specified prototype object and properties.
37
37
38
-
```js
38
+
```js live
39
39
constprototypeObject= {
40
40
greet() {
41
41
console.log('Hello, world!');
@@ -52,7 +52,7 @@ In this example, `newObject` is created with `prototypeObject` as its prototype.
52
52
53
53
Another way to implement the Prototype pattern in JavaScript is by using constructor functions and the `prototype` property.
Copy file name to clipboardExpand all lines: questions/explain-the-concept-of-the-singleton-pattern/en-US.mdx
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: Explain the concept of the Singleton pattern
6
6
7
7
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is useful when exactly one object is needed to coordinate actions across the system. In JavaScript, this can be implemented using closures or ES6 classes.
8
8
9
-
```js
9
+
```js live
10
10
classSingleton {
11
11
constructor() {
12
12
if (!Singleton.instance) {
@@ -40,7 +40,7 @@ There are several ways to implement the Singleton pattern in JavaScript. Here ar
Copy file name to clipboardExpand all lines: questions/how-can-you-create-custom-error-objects/en-US.mdx
+12-4Lines changed: 12 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: How can you create custom error objects?
6
6
7
7
To create custom error objects in JavaScript, you can extend the built-in `Error` class. This allows you to add custom properties and methods to your error objects. Here's a quick example:
8
8
9
-
```js
9
+
```js live
10
10
classCustomErrorextendsError {
11
11
constructor(message) {
12
12
super(message);
@@ -43,7 +43,7 @@ class CustomError extends Error {
43
43
44
44
You can add custom properties to your custom error class to provide more context about the error.
45
45
46
-
```js
46
+
```js live
47
47
classCustomErrorextendsError {
48
48
constructor(message, errorCode) {
49
49
super(message);
@@ -65,7 +65,7 @@ try {
65
65
66
66
You can also add custom methods to your custom error class to handle specific error-related logic.
67
67
68
-
```js
68
+
```js live
69
69
classCustomErrorextendsError {
70
70
constructor(message, errorCode) {
71
71
super(message);
@@ -89,7 +89,15 @@ try {
89
89
90
90
You can use the `instanceof` operator to check if an error is an instance of your custom error class.
91
91
92
-
```js
92
+
```js live
93
+
classCustomErrorextendsError {
94
+
constructor(message, errorCode) {
95
+
super(message);
96
+
this.name='CustomError';
97
+
this.errorCode= errorCode;
98
+
}
99
+
}
100
+
93
101
try {
94
102
thrownewCustomError('This is a custom error message', 404);
Copy file name to clipboardExpand all lines: questions/how-do-sets-and-maps-handle-equality-checks-for-objects/en-US.mdx
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: How do `Set`s and `Map`s handle equality checks for objects?
6
6
7
7
`Set`s and `Map`s in JavaScript handle equality checks for objects based on reference equality, not deep equality. This means that two objects are considered equal only if they reference the same memory location. For example, if you add two different object literals with the same properties to a `Set`, they will be treated as distinct entries.
8
8
9
-
```js
9
+
```js live
10
10
constset=newSet();
11
11
constobj1= { a:1 };
12
12
constobj2= { a:1 };
@@ -29,7 +29,7 @@ In JavaScript, `Set`s and `Map`s use reference equality to determine if two obje
29
29
30
30
When you add objects to a `Set`, the `Set` will only consider them equal if they are the same object reference.
31
31
32
-
```js
32
+
```js live
33
33
constset=newSet();
34
34
constobj1= { a:1 };
35
35
constobj2= { a:1 };
@@ -46,7 +46,7 @@ In this example, `obj1` and `obj2` have the same properties, but they are differ
46
46
47
47
Similarly, when you use objects as keys in a `Map`, the `Map` will only consider them equal if they are the same object reference.
Copy file name to clipboardExpand all lines: questions/how-do-you-get-the-query-string-values-of-the-current-page-in-javascript/en-US.mdx
+17-9Lines changed: 17 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,13 @@ title: How do you get the query string values of the current page in JavaScript?
6
6
7
7
To get the query string values of the current page in JavaScript, you can use the `URLSearchParams` object. First, create a `URLSearchParams` instance with `window.location.search`, then use the `get` method to retrieve specific query parameters. For example:
This will give you the value of the query parameter named `key`.
15
+
This will give you the value of the query parameter named `language`. If you look at the URL of this page, you can see that the `language` parameter is set to 'js'.
15
16
16
17
---
17
18
@@ -24,11 +25,14 @@ The `URLSearchParams` interface provides an easy way to work with query strings.
24
25
1.**Create a `URLSearchParams` instance**: Use `window.location.search` to get the query string part of the URL.
25
26
2.**Retrieve specific query parameters**: Use the `get` method to get the value of a specific query parameter.
0 commit comments