Skip to content

Executable coding blocks production-readiness - PR 3 (corrected) #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4352f7c
[auto] regenerate table of contents
github-actions[bot] Mar 7, 2025
fa1eb9b
exec: how-do-you-get-the-query-string-values-of-the-current-page-in-j…
tahachm Mar 8, 2025
2e1cfe7
exec: what-are-sets-and-maps-and-how-are-they-used
tahachm Mar 8, 2025
3e5339c
exec: what-are-the-differences-between-map-set-and-weakmap-weakset
tahachm Mar 8, 2025
5d7354a
exec: what-is-the-difference-between-a-map-object-and-a-plain-object-…
tahachm Mar 8, 2025
4a982ec
exec: how-do-sets-and-maps-handle-equality-checks-for-objects
tahachm Mar 8, 2025
914bcd4
exec: what-are-design-patterns-and-why-are-they-useful
tahachm Mar 8, 2025
3318310
exec: explain-the-concept-of-the-singleton-pattern
tahachm Mar 8, 2025
1d7f6e1
exec: explain-the-concept-of-the-prototype-pattern
tahachm Mar 8, 2025
5609a91
exec: what-are-symbols-used-for
tahachm Mar 8, 2025
d68dbab
exec: what-are-the-potential-issues-caused-by-hoisting
tahachm Mar 8, 2025
489b7fc
partial fix: describe-the-difference-between-a-cookie-sessionstorage-…
tahachm Mar 8, 2025
55a1055
exec: how-do-you-handle-errors-using-trycatch-blocks
tahachm Mar 8, 2025
89c5aac
exec: what-is-the-prototype-chain-and-how-does-it-work
tahachm Mar 8, 2025
499f32c
exec: why-you-might-want-to-create-static-class-members
tahachm Mar 8, 2025
0164368
exec: what-is-a-closure-and-how-why-would-you-use-one
tahachm Mar 8, 2025
6a19544
exec: what-are-the-potential-pitfalls-of-using-closures
tahachm Mar 8, 2025
dfbd09c
exec: what-are-the-different-types-of-errors-in-javascript
tahachm Mar 8, 2025
33079ab
exec: how-can-you-create-custom-error-objects
tahachm Mar 8, 2025
8f15bd0
exec: what-is-currying-and-how-does-it-work
tahachm Mar 8, 2025
58e7bf7
[auto] regenerate table of contents
github-actions[bot] Mar 21, 2025
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
49 changes: 28 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ In the book ["You Don't Know JS"](https://github.com/getify/You-Dont-Know-JS/tre

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.

```js
```js live
function outerFunction() {
const outerVar = 'I am outside of innerFunction';

Expand Down Expand Up @@ -1822,7 +1822,7 @@ On the other hand, `WeakSet` only allows objects as elements, and these object e

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.

```js
```js live
class Car {
static noOfWheels = 4;
static compare() {
Expand Down Expand Up @@ -1855,7 +1855,7 @@ Static members are useful under the following scenarios:

`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.

```js
```js live
let sym1 = Symbol();
let sym2 = Symbol('myKey');

Expand Down Expand Up @@ -2412,7 +2412,7 @@ console.log(combinedObj); // { a: 1, b: 2, c: 3, d: 4 }

`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.

```js
```js live
let sym1 = Symbol();
let sym2 = Symbol('myKey');

Expand Down Expand Up @@ -2568,11 +2568,11 @@ var bar = function () {

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:

```js
```js live
console.log(a); // undefined
var a = 5;

console.log(b); // ReferenceError: b is not defined
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;
```

Expand Down Expand Up @@ -4135,7 +4135,7 @@ Things to note are:

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`.

```js
```js live
function Person(name) {
this.name = name;
}
Expand Down Expand Up @@ -4401,7 +4401,7 @@ The main takeaway here is that `this` can be changed for a normal function, but

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.

```js
```js live
class Car {
static noOfWheels = 4;
static compare() {
Expand Down Expand Up @@ -4436,7 +4436,7 @@ In the book ["You Don't Know JS"](https://github.com/getify/You-Dont-Know-JS/tre

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.

```js
```js live
function outerFunction() {
const outerVar = 'I am outside of innerFunction';

Expand Down Expand Up @@ -5526,12 +5526,13 @@ window.location.replace('https://www.example.com');

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:

```js
```js live
const params = new URLSearchParams(window.location.search);
const value = params.get('key');
const value = params.get('language');
console.log(value);
```

This will give you the value of the query parameter named `key`.
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'.

<!-- Update here: /questions/how-do-you-get-the-query-string-values-of-the-current-page-in-javascript/en-US.mdx -->

Expand Down Expand Up @@ -5868,7 +5869,7 @@ try {

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:

```js
```js live
class CustomError extends Error {
constructor(message) {
super(message);
Expand Down Expand Up @@ -5928,7 +5929,7 @@ try {

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:

```js
```js live
function add(a) {
return function (b) {
return function (c) {
Expand All @@ -5938,8 +5939,13 @@ function add(a) {
}

const addOne = add(1);
console.log(addOne); // function object

const addOneAndTwo = addOne(2);
const result = addOneAndTwo(3); // result is 6
console.log(addOneAndTwo); // function object

const result = addOneAndTwo(3);
console.log(result); // Output: 6
```

<!-- Update here: /questions/what-is-currying-and-how-does-it-work/en-US.mdx -->
Expand Down Expand Up @@ -6033,10 +6039,11 @@ Currying transforms a function with multiple arguments into a sequence of functi

`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.

```js
```js live
// Set example
let mySet = new Set([1, 2, 3, 3]);
mySet.add(4); // Set {1, 2, 3, 4}
let mySet = new Set([1, 2, 3, 3]); // Set {1, 2, 3} (duplicate values are not added)
mySet.add(4);
console.log(mySet); // Set {1, 2, 3, 4}

// Map example
let myMap = new Map();
Expand Down Expand Up @@ -6149,7 +6156,7 @@ Both `Map` objects and plain objects in JavaScript can store key-value pairs, bu

`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.

```js
```js live
const set = new Set();
const obj1 = { a: 1 };
const obj2 = { a: 1 };
Expand Down Expand Up @@ -6527,7 +6534,7 @@ Design patterns are reusable solutions to common problems in software design. Th

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.

```js
```js live
class Singleton {
constructor() {
if (!Singleton.instance) {
Expand Down Expand Up @@ -6632,7 +6639,7 @@ myModule.publicMethod(); // Logs: I am private

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.

```js
```js live
const prototypeObject = {
greet() {
console.log('Hello, world!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ The CookieStore API is relatively new and may not be supported in all browsers (
- **Access**: Data is accessible within all tabs and windows of the same origin.
- **Security**: All JavaScript on the page have access to values within `localStorage`.

```js
```js live
// Set a value in localStorage.
localStorage.setItem('key', 'value');

Expand All @@ -123,7 +123,7 @@ localStorage.clear();
- **Access**: Data is only accessible within the current tab or window. Different tabs or windows with the same page will have different `sessionStorage` objects.
- **Security**: All JavaScript on the same page have access to values within `sessionStorage` for that page.

```js
```js live
// Set a value in sessionStorage.
sessionStorage.setItem('key', 'value');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: Explain the concept of the Prototype pattern

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.

```js
```js live
const prototypeObject = {
greet() {
console.log('Hello, world!');
Expand Down Expand Up @@ -35,7 +35,7 @@ In JavaScript, the Prototype pattern can be implemented using the `Object.create

The `Object.create` method creates a new object with the specified prototype object and properties.

```js
```js live
const prototypeObject = {
greet() {
console.log('Hello, world!');
Expand All @@ -52,7 +52,7 @@ In this example, `newObject` is created with `prototypeObject` as its prototype.

Another way to implement the Prototype pattern in JavaScript is by using constructor functions and the `prototype` property.

```js
```js live
function Person(name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: Explain the concept of the Singleton pattern

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.

```js
```js live
class Singleton {
constructor() {
if (!Singleton.instance) {
Expand Down Expand Up @@ -40,7 +40,7 @@ There are several ways to implement the Singleton pattern in JavaScript. Here ar

#### Using closures

```js
```js live
const Singleton = (function () {
let instance;

Expand All @@ -67,7 +67,7 @@ console.log(instance1 === instance2); // true

#### Using ES6 classes

```js
```js live
class Singleton {
constructor() {
if (!Singleton.instance) {
Expand Down
16 changes: 12 additions & 4 deletions questions/how-can-you-create-custom-error-objects/en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: How can you create custom error objects?

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:

```js
```js live
class CustomError extends Error {
constructor(message) {
super(message);
Expand Down Expand Up @@ -43,7 +43,7 @@ class CustomError extends Error {

You can add custom properties to your custom error class to provide more context about the error.

```js
```js live
class CustomError extends Error {
constructor(message, errorCode) {
super(message);
Expand All @@ -65,7 +65,7 @@ try {

You can also add custom methods to your custom error class to handle specific error-related logic.

```js
```js live
class CustomError extends Error {
constructor(message, errorCode) {
super(message);
Expand All @@ -89,7 +89,15 @@ try {

You can use the `instanceof` operator to check if an error is an instance of your custom error class.

```js
```js live
class CustomError extends Error {
constructor(message, errorCode) {
super(message);
this.name = 'CustomError';
this.errorCode = errorCode;
}
}

try {
throw new CustomError('This is a custom error message', 404);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: How do `Set`s and `Map`s handle equality checks for objects?

`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.

```js
```js live
const set = new Set();
const obj1 = { a: 1 };
const obj2 = { a: 1 };
Expand All @@ -29,7 +29,7 @@ In JavaScript, `Set`s and `Map`s use reference equality to determine if two obje

When you add objects to a `Set`, the `Set` will only consider them equal if they are the same object reference.

```js
```js live
const set = new Set();
const obj1 = { a: 1 };
const obj2 = { a: 1 };
Expand All @@ -46,7 +46,7 @@ In this example, `obj1` and `obj2` have the same properties, but they are differ

Similarly, when you use objects as keys in a `Map`, the `Map` will only consider them equal if they are the same object reference.

```js
```js live
const map = new Map();
const obj1 = { a: 1 };
const obj2 = { a: 1 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ title: How do you get the query string values of the current page in JavaScript?

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:

```js
```js live
const params = new URLSearchParams(window.location.search);
const value = params.get('key');
const value = params.get('language');
console.log(value);
```

This will give you the value of the query parameter named `key`.
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'.

---

Expand All @@ -24,11 +25,14 @@ The `URLSearchParams` interface provides an easy way to work with query strings.
1. **Create a `URLSearchParams` instance**: Use `window.location.search` to get the query string part of the URL.
2. **Retrieve specific query parameters**: Use the `get` method to get the value of a specific query parameter.

```js
```js live
const params = new URLSearchParams(window.location.search);
const value = params.get('key'); // Replace 'key' with the actual query parameter name
const value = params.get('key'); // Replace 'key' with the actual query parameter name (try 'language' or 'tab' for this page)
console.log(value);
```

If the query parameter does not exist, the `get` method returns `null`.

### Example

Consider a URL like `https://example.com?page=2&sort=asc`. To get the values of `page` and `sort`:
Expand All @@ -50,22 +54,26 @@ const values = params.getAll('key'); // Returns an array of values

### Checking for the existence of a parameter

You can use the `has` method to check if a query parameter exists:
You can use the `has` method to check if a query parameter exists. Try to check for the query parameters present in the URL of this page.

```js
```js live
const params = new URLSearchParams(window.location.search);
const hasPage = params.has('page'); // true or false
console.log(params.has('page')); // false
console.log(params.has('language')); // true
console.log(params.has('tab')); // true
```

### Iterating over all parameters

You can iterate over all query parameters using the `forEach` method:

```js
```js live
const params = new URLSearchParams(window.location.search);
params.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
// language: js
// tab: quiz
```

## Further reading
Expand Down
Loading