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
+30-23
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.
1857
1857
1858
-
```js
1858
+
```js live
1859
1859
let sym1 =Symbol();
1860
1860
let sym2 =Symbol('myKey');
1861
1861
@@ -2003,7 +2003,7 @@ Getters and setters are defined using the `get` and `set` keywords, respectively
2003
2003
2004
2004
Here's a code example demonstrating the use of getters and setters:
`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.
2412
2412
2413
-
```js
2413
+
```js live
2414
2414
let sym1 =Symbol();
2415
2415
let sym2 =Symbol('myKey');
2416
2416
@@ -2564,11 +2564,11 @@ var bar = function () {
2564
2564
2565
2565
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:
2566
2566
2567
-
```js
2567
+
```js live
2568
2568
console.log(a); // undefined
2569
2569
var a =5;
2570
2570
2571
-
console.log(b); // ReferenceError: b is not defined
2571
+
console.log(b); // ReferenceError: Cannot access 'b' before initialization
2572
2572
let b =10;
2573
2573
```
2574
2574
@@ -3647,7 +3647,7 @@ Getters and setters are defined using the `get` and `set` keywords, respectively
3647
3647
3648
3648
Here's a code example demonstrating the use of getters and setters:
3649
3649
3650
-
```js
3650
+
```js live
3651
3651
constperson= {
3652
3652
_name:'John Doe', // Private property
3653
3653
@@ -4122,7 +4122,7 @@ Things to note are:
4122
4122
4123
4123
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`.
4124
4124
4125
-
```js
4125
+
```js live
4126
4126
functionPerson(name) {
4127
4127
this.name= name;
4128
4128
}
@@ -4388,7 +4388,7 @@ The main takeaway here is that `this` can be changed for a normal function, but
4388
4388
4389
4389
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.
4390
4390
4391
-
```js
4391
+
```js live
4392
4392
classCar {
4393
4393
static noOfWheels =4;
4394
4394
staticcompare() {
@@ -4423,7 +4423,7 @@ In the book ["You Don't Know JS"](https://github.com/getify/You-Dont-Know-JS/tre
4423
4423
4424
4424
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`.
5516
+
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:
5851
5852
5852
-
```js
5853
+
```js live
5853
5854
classCustomErrorextendsError {
5854
5855
constructor(message) {
5855
5856
super(message);
@@ -5909,7 +5910,7 @@ try {
5909
5910
5910
5911
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:
@@ -6014,10 +6020,11 @@ Currying transforms a function with multiple arguments into a sequence of functi
6014
6020
6015
6021
`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.
6016
6022
6017
-
```js
6023
+
```js live
6018
6024
// Set example
6019
-
let mySet =newSet([1, 2, 3, 3]);
6020
-
mySet.add(4); // Set {1, 2, 3, 4}
6025
+
let mySet =newSet([1, 2, 3, 3]); // Set {1, 2, 3} (duplicate values are not added)
6026
+
mySet.add(4);
6027
+
console.log(mySet); // Set {1, 2, 3, 4}
6021
6028
6022
6029
// Map example
6023
6030
let myMap =newMap();
@@ -6130,7 +6137,7 @@ Both `Map` objects and plain objects in JavaScript can store key-value pairs, bu
6130
6137
6131
6138
`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.
6132
6139
6133
-
```js
6140
+
```js live
6134
6141
constset=newSet();
6135
6142
constobj1= { a:1 };
6136
6143
constobj2= { a:1 };
@@ -6508,7 +6515,7 @@ Design patterns are reusable solutions to common problems in software design. Th
6508
6515
6509
6516
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.
6510
6517
6511
-
```js
6518
+
```js live
6512
6519
classSingleton {
6513
6520
constructor() {
6514
6521
if (!Singleton.instance) {
@@ -6613,7 +6620,7 @@ myModule.publicMethod(); // Logs: I am private
6613
6620
6614
6621
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-2
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-3
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-3
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-4
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-3
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-9
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