From 4c0c720acb96095618a287d3efbfe1b25e6c4069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christof=20K=C3=A4lin?= Date: Thu, 17 Apr 2025 14:45:39 +0200 Subject: [PATCH 1/2] Formatting, typos and some infos about deprecations from 149-200 --- README.md | 384 +++++++++++++++++++++++++++--------------------------- 1 file changed, 194 insertions(+), 190 deletions(-) diff --git a/README.md b/README.md index 0d6b4faa..6188cda4 100644 --- a/README.md +++ b/README.md @@ -3003,14 +3003,14 @@ 149. ### How do you generate random integers - You can use Math.random() with Math.floor() to return random integers. For example, if you want generate random integers between 1 to 10, the multiplication factor should be 10, + You can use `Math.random()` with `Math.floor()` to return random integers. For example, if you want generate random integers between 1 to 10, the multiplication factor should be 10, ```javascript Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 10 Math.floor(Math.random() * 100) + 1; // returns a random integer from 1 to 100 ``` - **Note:** Math.random() returns a random number between 0 (inclusive), and 1 (exclusive) + **Note:** `Math.random()` returns a random number between 0 (inclusive), and 1 (exclusive) **[⬆ Back to Top](#table-of-contents)** @@ -3030,7 +3030,7 @@ 151. ### What is tree shaking - Tree shaking is a form of dead code elimination. It means that unused modules will not be included in the bundle during the build process and for that it relies on the static structure of ES2015 module syntax,( i.e. import and export). Initially this has been popularized by the ES2015 module bundler `rollup`. + Tree shaking is a form of dead code elimination. It means that unused modules will not be included in the bundle during the build process and for that it relies on the static structure of ES2015 module syntax,( i.e. import and export). Initially this has been popularized by the ES2015 module bundler `rollup`, these days practically all bundlers use this technique. **[⬆ Back to Top](#table-of-contents)** @@ -3064,16 +3064,16 @@ 155. ### What are the string methods that accept Regular expression - There are six string methods: search(), replace(), replaceAll(), match(), matchAll(), and split(). + There are six string methods: `search()`, `replace()`, `replaceAll()`, `match()`, `matchAll()`, and `split()`. - The search() method uses an expression to search for a match, and returns the position of the match. + The `search()` method uses an expression to search for a match, and returns the position of the match. ```javascript var msg = "Hello John"; var n = msg.search(/John/i); // 6 ``` - The replace() and replaceAll() methods are used to return a modified string where the pattern is replaced. + The `replace()` and `replaceAll()` methods are used to return a modified string where the pattern is replaced. ```javascript var msg = "ball bat"; @@ -3081,7 +3081,7 @@ var n2 = msg.replaceAll(/b/i, "c"); // call cat ``` - The match() and matchAll() methods are used to return the matches when matching a string against a regular expression. + The `match()` and `matchAll()` methods are used to return the matches when matching a string against a regular expression. ```javascript var msg = "Hello John"; @@ -3089,7 +3089,7 @@ var n2 = msg.matchAll(/[A-Z]/g); // this returns an iterator ``` - The split() method is used to split a string into an array of substrings, and returns the new array. + The `split()` method is used to split a string into an array of substrings, and returns the new array. ```javascript var msg = "Hello John"; @@ -3100,7 +3100,7 @@ 156. ### What are modifiers in regular expression - Modifiers can be used to perform case-insensitive and global searches. Let's list down some of the modifiers, + Modifiers can be used to perform case-insensitive and global searches. Let's list some of the modifiers, | Modifier | Description | | -------- | ------------------------------------------------------- | @@ -3154,7 +3154,7 @@ 159. ### How do you search a string for a pattern - You can use the test() method of regular expression in order to search a string for a pattern, and return true or false depending on the result. + You can use the `test()` method of regular expression in order to search a string for a pattern, and return true or false depending on the result. ```javascript var pattern = /you/; @@ -3176,7 +3176,7 @@ 161. ### How do you change the style of a HTML element - You can change inline style or classname of a HTML element using javascript + You can change inline style or classname of a HTML element using javascript DOM-manipulation 1. **Using style property:** You can modify inline style using style property @@ -3294,7 +3294,7 @@ 169. ### How do you make synchronous HTTP request - Browsers provide an XMLHttpRequest object which can be used to make synchronous HTTP requests from JavaScript + Browsers provide an XMLHttpRequest object which can be used to make synchronous HTTP requests from JavaScript. ```javascript function httpGet(theUrl) { @@ -3304,6 +3304,7 @@ return xmlHttpReq.responseText; } ``` + **[⬆ Back to Top](#table-of-contents)** @@ -3323,9 +3324,12 @@ } ``` + Today this is considered deprecated, because an async `fetch` call (in browsers later than 2016) is simpler and more robust. + + **[⬆ Back to Top](#table-of-contents)** -171. ### How do you convert date to another timezone in javascript +172. ### How do you convert date to another timezone in javascript You can use the toLocaleString() method to convert dates in one timezone to another. For example, let's convert current date to British English timezone as below, @@ -3335,7 +3339,7 @@ **[⬆ Back to Top](#table-of-contents)** -172. ### What are the properties used to get size of window +173. ### What are the properties used to get size of window You can use innerWidth, innerHeight, clientWidth, clientHeight properties of windows, document element and document body objects to find the size of a window. Let's use them combination of these properties to calculate the size of a window or document, @@ -3353,22 +3357,22 @@ **[⬆ Back to Top](#table-of-contents)** -173. ### What is a conditional operator in javascript +174. ### What is a conditional operator in javascript - The conditional (ternary) operator is the only JavaScript operator that takes three operands which acts as a shortcut for if statements. + The conditional (ternary) operator is the only JavaScript operator that takes three operands which acts as a shortcut for `if` statements. ```javascript var isAuthenticated = false; console.log( isAuthenticated ? "Hello, welcome" : "Sorry, you are not authenticated" - ); //Sorry, you are not authenticated + ); // Sorry, you are not authenticated ``` **[⬆ Back to Top](#table-of-contents)** -174. ### Can you apply chaining on conditional operator +175. ### Can you apply chaining on conditional operator - Yes, you can apply chaining on conditional operators similar to **if … else if … else if … else** chain. The syntax is going to be as below, + Yes, you can apply chaining on conditional operators similar to **`if … else if … else if … else`** chain. The syntax is going to be as below, ```javascript function traceValue(someParam) { @@ -3398,7 +3402,7 @@ **[⬆ Back to Top](#table-of-contents)** -175. ### What are the ways to execute javascript after page load +176. ### What are the ways to execute javascript after a page load You can execute javascript after page load in many different ways, @@ -3422,9 +3426,9 @@ **[⬆ Back to Top](#table-of-contents)** -176. ### What is the difference between proto and prototype +177. ### What is the difference between proto and prototype - The `__proto__` object is the actual object that is used in the lookup chain to resolve methods, etc. Whereas `prototype` is the object that is used to build `__proto__` when you create an object with new. + The `__proto__` object is the actual object that is used in the lookup chain to resolve methods, etc. Whereas `prototype` is the object that is used to build `__proto__` when you create an object with the `new` operator (a special variant of a function call). ```javascript new Employee().__proto__ === Employee.prototype; @@ -3435,16 +3439,16 @@ | feature | Prototype | proto | | ---------- | ------------------------------------------------------------ | ---------------------------------------------------------- | - | Access | All the function constructors have prototype properties. | All the objects have \_\_proto\_\_ property | + | Access | All function constructors have prototype properties. | All objects have \_\_proto\_\_ property | | Purpose | Used to reduce memory wastage with a single copy of function | Used in lookup chain to resolve methods, constructors etc. | | ECMAScript | Introduced in ES6 | Introduced in ES5 | | Usage | Frequently used | Rarely used | **[⬆ Back to Top](#table-of-contents)** -177. ### Can you give an example of when you really need a semicolon +178. ### Can you give an example of when you really need a semicolon - It is recommended to use semicolons after every statement in JavaScript. For example, in the below case it throws an error ".. is not a function" at runtime due to missing semicolon. + It is recommended to use semicolons after every statement in JavaScript. For example, in the below case (that is an IIFE = Immediately Invoked Function Expression) it throws an error ".. is not a function" at runtime due to missing semicolon. ```javascript // define a function @@ -3474,9 +3478,9 @@ **[⬆ Back to Top](#table-of-contents)** -178. ### What is a freeze method +179. ### What is the freeze method - The **freeze()** method is used to freeze an object. Freezing an object does not allow adding new properties to an object, prevents removing, and prevents changing the enumerability, configurability, or writability of existing properties. i.e. It returns the passed object and does not create a frozen copy. + The **`freeze()`** method is used to freeze an object. Freezing an object does not allow adding new properties to an object, prevents removing, and prevents changing the enumerability, configurability, or writability of existing properties. i.e. It returns the passed object and does not create a frozen copy. ```javascript const obj = { @@ -3508,7 +3512,7 @@ **[⬆ Back to Top](#table-of-contents)** -179. ### What is the purpose of freeze method +180. ### What is the purpose of freeze method Below are the main benefits of using freeze method, @@ -3517,15 +3521,15 @@ **[⬆ Back to Top](#table-of-contents)** -180. ### Why do I need to use freeze method +181. ### Why do I need to use freeze method In the Object-oriented paradigm, an existing API contains certain elements that are not intended to be extended, modified, or re-used outside of their current context. Hence it works as the `final` keyword which is used in various languages. **[⬆ Back to Top](#table-of-contents)** -181. ### How do you detect a browser language preference +182. ### How do you detect a browser language preference - You can use navigator object to detect a browser language preference as below, + You can use the navigator object to detect a browser language preference as below, ```javascript var language = @@ -3538,7 +3542,7 @@ **[⬆ Back to Top](#table-of-contents)** -182. ### How to convert string to title case with javascript +183. ### How to convert a string to title case with javascript Title case means that the first letter of each word is capitalized. You can convert a string to title case using the below function, @@ -3553,7 +3557,7 @@ **[⬆ Back to Top](#table-of-contents)** -183. ### How do you detect javascript disabled in the page +184. ### How do you detect if javascript is disabled on the page You can use the `