### Basic usage of trimStart() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart This example demonstrates trimming whitespace from the start of a string, leaving trailing whitespace untouched. The original string remains unchanged. ```javascript const greeting = ' Hello world! '; console.log(greeting.trimStart()); // Expected output: 'Hello world! ' console.log(greeting); // Expected output: ' Hello world! ' ``` -------------------------------- ### Using the constructor Example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor A practical example demonstrating the usage of the `constructor` method within a class. ```APIDOC ## Example: Using the constructor ```javascript class Person { constructor(name, age) { this.name = name; this.age = age; } greet() { console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`); } } const person1 = new Person('Alice', 30); person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. ``` ``` -------------------------------- ### Basic Proxy with get handler Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy This example demonstrates a basic proxy that returns a default value when a property is not found on the target object. It uses the `get()` handler to intercept property access. ```javascript const target = {}; const handler = { get(target, property, receiver) { if (property === 'a') { return 1; } return 37; } }; const proxy = new Proxy(target, handler); console.log(proxy.a); // Expected output: 1 console.log(proxy.b); // Expected output: 37 ``` -------------------------------- ### Bitwise AND example calculation Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/and Demonstrates a numerical example of a bitwise AND operation. ```plaintext 5 0101 1 0001 ---- 1 0001 ``` -------------------------------- ### JavaScriptCore Stack Trace Example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack Example of a stack trace format from JavaScriptCore engine. ```javascript baz@filename.js:10:24 bar@filename.js:6:6 foo@filename.js:2:6 global code@filename.js:13:4 ``` -------------------------------- ### V8 Stack Trace Example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack Example of a stack trace format from V8 engine. ```javascript Error at baz (filename.js:10:15) at bar (filename.js:6:3) at foo (filename.js:2:3) at filename.js:13:1 ``` -------------------------------- ### Get the square root of 1/2 Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/SQRT1_2 This example demonstrates how to get the value of Math.SQRT1_2 and log it to the console. ```javascript function getRoot1Over2() { return Math.SQRT1_2; } console.log(getRoot1Over2()); // Expected output: 0.7071067811865476 ``` -------------------------------- ### Examples Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array Demonstrates different ways to create an Int32Array. ```APIDOC ## Examples ### Different ways to create an Int32Array ``` -------------------------------- ### Get the day of the week Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate/dayOfWeek You can get the day of the week using the `dayOfWeek` accessor property. This example shows how to get the day of the week for a specific date. ```javascript const today = Temporal.PlainDate.from("2023-10-26"); console.log(today.dayOfWeek); // 4 ``` -------------------------------- ### Basic Usage Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart Demonstrates how to use the trimStart() method to remove whitespace from the beginning of a string. ```APIDOC ## trimStart() ### Description Removes whitespace from the beginning of a string. ### Syntax ```javascript str.trimStart() ``` ### Parameters None. ### Return value A new string representing `str` stripped of whitespace from its beginning. ### Examples #### Using trimStart() ```javascript const greeting = ' Hello world '; console.log(greeting.trimStart()); // Expected output: 'Hello world ' const whitespace = ' '; console.log(whitespace.trimStart()); // Expected output: '' const noWhitespace = 'Hello world'; console.log(noWhitespace.trimStart()); // Expected output: 'Hello world' ``` ### Aliasing `trimLeft()` is an alias of `trimStart()`. ``` -------------------------------- ### Get milliseconds from a Date object Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMilliseconds This example demonstrates how to get the milliseconds from a Date object. If milliseconds are not specified, they default to 0. ```javascript const xmas95 = new Date("12/25/1995 13:00:00"); const milliseconds = xmas95.getMilliseconds(); console.log(milliseconds); ``` -------------------------------- ### Get the timezone offset in minutes Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset This example demonstrates how to get the difference in minutes between UTC and the local time zone for a given date. ```javascript const today = new Date(); const offset = today.getTimezoneOffset(); console.log(`The timezone offset is: ${offset} minutes.`); // For example, if the local time zone is UTC-5, the output might be: // The timezone offset is: 300 minutes. ``` -------------------------------- ### String.prototype.padStart() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart Pads the current string with a given string (repeated and/or truncated, if needed) so that the resulting string has a given length. The padding is applied from the start of this string. ```APIDOC ## String.prototype.padStart() ### Description The `padStart()` method pads the current string with another string (multiple times, if necessary) until the resulting string reaches the given length. The padding is applied from the start of the current string. ### Parameters * `targetLength` (number) - The length of the resulting string once the current `str` has been padded. If the value is less than or equal to `str.length`, then `str` is returned as-is. * `padString` (string, optional) - The string to pad the current `str` with. If `padString` is too long to stay within `targetLength`, it will be truncated from the end. The default value is the space character (' '). ### Return value A `String` of the specified `targetLength` with `padString` applied at the start. ### Examples #### Fixed width string number conversion ```javascript const str = '5'; console.log(str.padStart(5)); // " 5" console.log(str.padStart(5, '0')); // "00005" console.log(str.padStart(5, '123')); // "123125" console.log(str.padStart(3, '12345')); // "123" ``` ### Browser compatibility | Feature | Chrome | Firefox | Safari | Edge | IE | | :------ | :----- | :------ | :----- | :--- | :-- | | Basic | 47 | 41 | 10 | 12 | No | ### Specifications | Specification | Update | | :------------ | :------ | | ECMAScript® 2027 Language Specification | `#sec-string.prototype.padstart` | ### See also * Polyfill of `String.prototype.padStart` in `core-js` * es-shims polyfill of `String.prototype.padStart` * `String.prototype.padEnd()` ``` -------------------------------- ### Remove 2 elements starting from index 2 Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice This example demonstrates removing a specified number of elements starting from a given index. ```javascript const myFish = ["angel", "clown", "mandarin", "sturgeon"]; const removed = myFish.splice(2, 2); console.log(removed); // ["mandarin", "sturgeon"] console.log(myFish); // ["angel", "clown"] ``` -------------------------------- ### Remove all elements starting from index 2 Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice When deleteCount is omitted or greater than or equal to the number of elements after start, all elements from start to the end are deleted. This example removes all elements from index 2 onwards. ```javascript const myFish = ["angel", "clown", "mandarin", "sturgeon"]; const removed = myFish.splice(2); console.log(removed); // ["mandarin", "sturgeon"] console.log(myFish); // ["angel", "clown"] ``` -------------------------------- ### Create a basic Proxy Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy This example demonstrates creating a proxy with an empty handler, resulting in behavior identical to the original target object. ```javascript const target = { message1: "hello", }; const handler = {}; const proxy = new Proxy(target, handler); console.log(proxy.message1); // "hello" ``` -------------------------------- ### Get seconds from a Date object Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getSeconds This example demonstrates how to get the seconds component from a Date object. The 'seconds' variable will hold the value 30. ```javascript const xmas95 = new Date("12/25/95 14:15:30"); console.log(xmas95.getSeconds()); // Expected output: 30 ``` -------------------------------- ### Using resolvedOptions() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/resolvedOptions This example demonstrates creating a PluralRules object with specific options and then logging the resolved options to the console. It shows how the constructor normalizes and fills in default values. ```javascript const pl = new Intl.PluralRules("en", { type: "cardinal", minimumFractionDigits: 2, maximumFractionDigits: 2, }); console.log(pl.resolvedOptions()); /* { locale: "en", type: "cardinal", minimumFractionDigits: 2, maximumFractionDigits: 2 } */ const plOrdinal = new Intl.PluralRules("fr", { type: "ordinal", minimumSignificantDigits: 1, maximumSignificantDigits: 3, }); console.log(plOrdinal.resolvedOptions()); /* { locale: "fr", type: "ordinal", minimumSignificantDigits: 1, maximumSignificantDigits: 3 } */ ``` -------------------------------- ### Displaying the first image loaded Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any This example uses Promise.any() to fetch multiple images and display the first one that successfully loads. It demonstrates a practical use case for Promise.any() in scenarios where only one successful outcome is needed from multiple attempts. ```javascript function fetchImage(url) { return fetch(url).then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.blob(); }); } const imageUrls = [ 'https://developer.mozilla.org/samples/image-set/example-1.jpg', 'https://developer.mozilla.org/samples/image-set/example-2.jpg', 'https://developer.mozilla.org/samples/image-set/example-3.jpg' ]; const imagePromises = imageUrls.map(url => fetchImage(url)); Promise.any(imagePromises) .then(firstBlob => { const imageUrl = URL.createObjectURL(firstBlob); const img = document.createElement('img'); img.src = imageUrl; document.body.appendChild(img); console.log('Displayed the first loaded image.'); }) .catch(error => { console.error('All images failed to load:', error); }); ``` -------------------------------- ### Get the UTF-16 code unit of a character Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt This example demonstrates how to get the UTF-16 code unit for the character 'A' at index 0 of the string. The result is 65. ```javascript const str = "A"; console.log(str.charCodeAt(0)); // 65 ``` -------------------------------- ### Constructor Initialization Steps Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor Describes the sequence of steps involved when using `new` with a class constructor, including `super()` calls and field initialization. ```APIDOC ## Constructor Initialization Steps Using `new` on a class goes through the following steps: 1. (If it's a derived class) The `constructor` body before the `super()` call is evaluated. This part should not access `this` because it's not yet initialized. 2. (If it's a derived class) The `super()` call is evaluated, which initializes the parent class through the same process. 3. The current class's fields are initialized. 4. The `constructor` body after the `super()` call (or the entire body, if it's a base class) is evaluated. ``` -------------------------------- ### Get the day of the month using getUTCDate() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDate This example demonstrates how to get the day of the month for the current date according to universal time and assign it to a variable. ```javascript const today = new Date(); const dayOfMonth = today.getUTCDate(); console.log(dayOfMonth); ``` -------------------------------- ### Bitwise OR Example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/or Demonstrates the bitwise OR operation. Note that this example is illustrative and cannot be run directly due to SharedArrayBuffer security requirements. ```javascript 5 | 1 ``` -------------------------------- ### Redefine all property accessors with get() trap Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy This example illustrates redefining all property accessors using the `get()` trap. It logs the property and returns a modified value. ```javascript const target = { name: "John Doe", age: 30 }; const handler2 = { get(target, prop, receiver) { if (prop === 'age') { return `User is ${target[prop]} years old`; } return Reflect.get(target, prop, receiver); } }; const proxy2 = new Proxy(target, handler2); console.log(proxy2.name); // "John Doe" console.log(proxy2.age); // "User is 30 years old" ``` -------------------------------- ### Object.prototype.constructor Examples Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor Demonstrates how the `constructor` property references the correct constructor function for various object types created using literals or the `new` keyword. ```javascript const o1 = {}; o1.constructor === Object; // true const o2 = new Object(); o2.constructor === Object; // true const a1 = []; a1.constructor === Array; // true const a2 = new Array(); a2.constructor === Array; // true const n = 3; n.constructor === Number; // true ``` -------------------------------- ### Trap getting a property value Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get This example demonstrates how to trap the retrieval of a property value using the `get` handler. It logs the access and returns the property's value. ```javascript const handler = { get(target, property, receiver) { console.log(`Property '${property}' will be accessed. `); // You can perform custom logic here before returning the value. // For example, you could return a default value if the property doesn't exist. return Reflect.get(...arguments); } }; const target = { message1: "hello", message2: "world" }; const proxy = new Proxy(target, handler); console.log(proxy.message1); // Expected output: // Property 'message1' will be accessed. // hello console.log(proxy.message2); // Expected output: // Property 'message2' will be accessed. // world ``` -------------------------------- ### Get day of the week for a specific date Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay This example demonstrates how to get the day of the week for a specific date. The return value is an integer from 0 (Sunday) to 6 (Saturday). ```javascript const xmas95 = new Date("December 25, 1995 13:00:00"); const weekday = xmas95.getDay(); console.log(weekday); // Expected output: 1 ``` -------------------------------- ### Importing a Module Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import This example demonstrates importing all exports from a module. Ensure the module exporting the function is correctly defined. ```javascript import { prime } from "./prime.js"; console.log(prime(10)); // 2, 3, 5, 7 ``` -------------------------------- ### Get UTC minutes from current time Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMinutes This example gets the current date and time, then extracts the minutes portion according to UTC and logs it to the console. It demonstrates the basic usage of getUTCMinutes(). ```javascript const now = new Date(); const minutes = now.getUTCMinutes(); console.log(`Current UTC minutes: ${minutes}`); ``` -------------------------------- ### Bitwise AND operation example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND Demonstrates the bitwise AND operation between two numbers, showing the conversion to binary and the resulting binary and decimal values. ```javascript 9 (base 10) = 00000000000000000000000000001001 (base 2) 14 (base 10) = 00000000000000000000000000001110 (base 2) -------------------------------- 14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10) ``` -------------------------------- ### Using getUTCMonth() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth This example demonstrates how to get the month portion of the current date using getUTCMonth(). ```javascript const today = new Date(); const month = today.getUTCMonth(); console.log(month); // Expected output (will vary depending on the current date): // 7 (for August) ``` -------------------------------- ### Create and read an 8-bit signed integer Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getInt8 This example demonstrates how to create a DataView, set an 8-bit signed integer at a specific offset, and then read it back. ```javascript const buffer = new ArrayBuffer(16); const view = new DataView(buffer); view.setInt8(1, 127); // Max signed 8-bit integer console.log(view.getInt8(1)); ``` -------------------------------- ### Violating an invariant with get() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get This example shows how violating the invariant for non-writable, non-configurable own data properties results in a TypeError. The `get` trap attempts to return a different value than the target's property. ```javascript const handler = { get(target, property, receiver) { // Violates invariant: target property is non-writable, non-configurable // but the trap returns a different value. if (property === "a") { return "different value"; } return Reflect.get(...arguments); } }; const target = {}; Object.defineProperty(target, "a", { value: "original value", writable: false, configurable: false }); const proxy = new Proxy(target, handler); try { console.log(proxy.a); } catch (e) { console.error(e); // TypeError: 'get' on proxy: handler violates the Chrome's invariant } ``` -------------------------------- ### Using maxByteLength Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/maxByteLength This example demonstrates how to create a resizable SharedArrayBuffer and access its maxByteLength. ```APIDOC ## Using maxByteLength In this example, we create a 8-byte buffer that is resizable to a max length of 16 bytes, then return its `maxByteLength`: ```javascript const sab = new SharedArrayBuffer(8, { maxByteLength: 16 }); console.log(sab.maxByteLength); // 16 ``` ``` -------------------------------- ### Dynamic Import Example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/import_decl_module_top_level Demonstrates how to use dynamic import() for conditional or on-demand module loading, which can be used when static import is not suitable. ```javascript if (someCondition) { import('./myModule.js').then(module => { // Use the module }); } ``` -------------------------------- ### Get code point at a specific index Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt This example demonstrates how to get the Unicode code point of a character at a specific index. It shows how `codePointAt()` handles characters that span multiple UTF-16 code units. ```javascript const str = "😀"; // Grinning Face emoji console.log(str.codePointAt(0)); // 128512 (0x1F600) console.log(str.codePointAt(1)); // 56832 (0xDE00) - trailing surrogate const str2 = "A"; // Latin Capital Letter A console.log(str2.codePointAt(0)); // 65 (0x41) const str3 = "₻7"; // Mathematical Alphanumeric Symbol Seven console.log(str3.codePointAt(0)); // 13975 (0x36B7) console.log(str3.codePointAt(1)); // 56311 (0xDCB7) - trailing surrogate ``` -------------------------------- ### Bitwise XOR Example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR Demonstrates the bitwise XOR operation on two numbers, showing the binary representation and the resulting integer. ```javascript 9 (base 10) = 00000000000000000000000000001001 (base 2) 14 (base 10) = 00000000000000000000000000001110 (base 2) -------------------------------- 14 ^ 9 (base 10) = 00000000000000000000000000000111 (base 2) = 7 (base 10) ``` -------------------------------- ### Using Math.sumPrecise() with an array Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sumPrecise This is a basic example of how to use Math.sumPrecise() with an array of numbers to get their precise sum. ```javascript const numbers = [1.1, 2.2, 3.3]; const preciseSum = Math.sumPrecise(numbers); console.log(preciseSum); // 6.6 ``` -------------------------------- ### Using trimStart() with various whitespace characters Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart This example shows how trimStart() removes various types of whitespace, including spaces, tabs, and line breaks, from the beginning of a string. ```javascript const str = '\t\n Some text'; console.log(str.trimStart()); // Expected output: 'Some text' ``` -------------------------------- ### Using Reflect.getPrototypeOf() to get the prototype of an object Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect This example demonstrates how to use Reflect.getPrototypeOf() to retrieve the prototype of a given object. ```javascript const target = {}; const proto = {}; Object.setPrototypeOf(target, proto); console.log(Reflect.getPrototypeOf(target) === proto); // Expected output: true ``` -------------------------------- ### Invalid Variable Name Example Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Identifier_after_number Demonstrates an invalid variable declaration that starts with a numeric literal, which is not allowed in JavaScript. ```javascript let 1variable = 1; ``` -------------------------------- ### Creating a function with the Function constructor Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function This example demonstrates how to create a function using the Function constructor. The last argument is the function body, and preceding arguments are parameter names. ```javascript const sum = new Function('a', 'b', 'return a + b;'); console.log(sum(2, 3)); // 5 ``` -------------------------------- ### Standard source map syntax Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_source_map_pragma This example demonstrates the standard and recommended syntax for source map pragmas using `//#`. This should be used instead of the deprecated `//@` syntax. ```javascript //# sourceMappingURL=example.js.map ``` ```javascript //# sourceURL=example.js ``` -------------------------------- ### Displaying RegExp string value Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString This example shows how to get the string representation of a RegExp object using its toString() method. ```javascript const regex = /hello\nworld/gi; console.log(regex.toString()); // "/hello\nworld/gi" ``` -------------------------------- ### Using take() to get the first three Fibonacci numbers Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/take This example demonstrates how to use `take()` to get a specific number of elements from an iterator. It defines a Fibonacci sequence generator and then uses `take(3)` to log the first three terms. ```javascript function* fibonacci() { let [prev, curr] = [0, 1]; while (true) { yield curr; [prev, curr] = [curr, prev + curr]; } } const firstThree = fibonacci().take(3); for (const num of firstThree) { console.log(num); } // Output: // 1 // 1 // 2 ``` -------------------------------- ### Basic Math.imul() examples Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul Demonstrates the basic usage of Math.imul() with positive, negative, and large integer values. ```javascript console.log(Math.imul(3, 4)); // Expected output: 12 console.log(Math.imul(-5, 12)); // Expected output: -60 console.log(Math.imul(0xffffffff, 5)); // Expected output: -5 console.log(Math.imul(0xfffffffe, 5)); // Expected output: -10 ``` -------------------------------- ### Return the last value of a typed array Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at This example demonstrates how to get the last element of a typed array using a positive index. ```javascript const numbers = new Uint8Array([10, 20, 30, 40, 50]); // Get the last element (index 4) console.log(numbers.at(4)); // 50 // Get the last element using negative indexing console.log(numbers.at(-1)); // 50 ``` -------------------------------- ### Basic cases for Math.pow() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow Shows fundamental examples of Math.pow() with positive integer exponents. ```javascript // Basic cases Math.pow(7, 2); // 49 Math.pow(7, 3); // 343 Math.pow(2, 10); // 1024 ``` -------------------------------- ### Basic string splitting Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split Demonstrates splitting a string by a space, 'undefined', and a comma. Shows how `split()` handles empty strings and different separators. ```javascript const str = "Oh brave new world that has such people in it."; const words = str.split(' '); console.log(words[2]); // "new" const freqArray = str.split(); console.log(freqArray[0]); // "Oh brave new world that has such people in it." const months = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"; const monthList = months.split(','); console.log(monthList[4]); // "May" ``` -------------------------------- ### Basic usage of toLocaleString() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth/toLocaleString This example demonstrates the basic usage of the toLocaleString() method to get a locale-specific string representation of a Temporal.PlainYearMonth object. ```APIDOC ## Temporal.PlainYearMonth.prototype.toLocaleString() ### Description Returns a string representing the given date (year and month) in a language- or locale-specific format. ### Method `toLocaleString()` ### Parameters This method does not accept any parameters. ### Request Example ```javascript const yearMonth = Temporal.PlainYearMonth.from("2024-07"); const localeString = yearMonth.toLocaleString(); console.log(localeString); // Output will vary based on the default locale ``` ### Response #### Success Response - **string**: A string representing the year and month in the current locale's format. ``` -------------------------------- ### Using Reflect.getOwnPropertyDescriptor() to get property descriptor Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect This example shows how to use Reflect.getOwnPropertyDescriptor() to retrieve the property descriptor for a given property on an object. ```javascript const target = { message1: "hello" }; const descriptor = Reflect.getOwnPropertyDescriptor(target, "message1"); console.log(descriptor); // Expected output: { value: 'hello', writable: true, enumerable: true, configurable: true } ``` -------------------------------- ### Using Options Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString Demonstrates how to customize the number formatting using the 'options' parameter, such as specifying currency, minimum/maximum fraction digits, and style. ```APIDOC ## Using options ### Description The results provided by `toLocaleString()` can be customized using the `options` parameter. ### Example ```javascript const number = 123456.789; // Formatting as currency console.log(number.toLocaleString('en-US', { style: 'currency', currency: 'USD' })); // Expected output: "$123,456.79" // Formatting with minimum and maximum fraction digits console.log(number.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 3 })); // Expected output: "123,456.789" // Formatting as a percentage console.log((0.123).toLocaleString('en-US', { style: 'percent' })); // Expected output: "12%" ``` ``` -------------------------------- ### Using Reflect.ownKeys() to get own property keys Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect This example demonstrates how to use Reflect.ownKeys() to retrieve an array of an object's own property keys. ```javascript const target = { message1: "hello", message2: "world" }; const ownKeys = Reflect.ownKeys(target); console.log(ownKeys); // Expected output: ["message1", "message2"] ``` -------------------------------- ### Syntax for Object.create() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create Shows the two possible syntaxes for Object.create(): one with only the prototype and another with both the prototype and a properties object. ```javascript Object.create(proto) Object.create(proto, propertiesObject) ``` -------------------------------- ### Get non-enumerable properties only Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames This example filters the properties obtained from Object.getOwnPropertyNames() to exclude enumerable properties, effectively returning only the non-enumerable own properties of an object. ```javascript const car = Object.create({}, { getPrice: { value: function() { return 50000; }, enumerable: true }, getYear: { value: function() { return 2005; }, enumerable: true }, registration: { value: 'ABC-123', enumerable: false } }); const nonEnumerable = Object.getOwnPropertyNames(car).filter((key) => { return !car.propertyIsEnumerable(key); }); console.log(nonEnumerable); // ['registration'] ``` -------------------------------- ### Using type 'calendar' with Intl.DisplayNames() Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames Example of using the 'calendar' type with Intl.DisplayNames to get localized calendar names. The default locale is used. ```javascript const options = { type: 'calendar', // other options like localeMatcher, style, fallback, languageDisplay }; const names = new Intl.DisplayNames(undefined, options); console.log(names.of('gregory')); // "Gregorian" console.log(names.of('hebrew')); // "Hebrew" console.log(names.of('islamic')); // "Islamic" ```