Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 447 of 534
Is it possible to de-structure to already-declared variables? In JavaScript?
Yes, it is possible to destructure to already-declared variables in JavaScript. However, you must wrap the destructuring assignment in parentheses to avoid syntax errors. The Problem When variables are already declared, JavaScript cannot distinguish between a block statement and destructuring assignment: Destructuring Problem let name, age; ...
Read MoreHow to transform JavaScript arrays using maps?
JavaScript's map() method creates a new array by transforming each element of the original array using a callback function. It's essential for functional programming and data transformation. Syntax array.map(callback(element, index, array)) Parameters callback - Function that transforms each element element - Current array element being processed index - Index of the current element (optional) array - The original array (optional) Basic Example: Squaring Numbers Transform Arrays with Map ...
Read MoreFunction borrowing in JavaScript.
Function borrowing in JavaScript allows objects to use methods from other objects without inheriting from them. This is achieved using call(), apply(), and bind() methods. Understanding Function Borrowing When an object doesn't have a particular method but needs to use it, it can "borrow" that method from another object. The borrowed method executes in the context of the borrowing object using the this keyword. Using call() Method The call() method invokes a function with a specific this context and individual arguments. Function Borrowing with call() ...
Read MoreNesting template strings in JavaScript
Nesting template strings in JavaScript allows you to embed one template literal inside another. This is useful when building complex strings with dynamic content or when passing template literals as function arguments. What are Nested Template Strings? Nested template strings occur when you place one template literal (using backticks) inside another. The inner template string is evaluated first, then the outer one. Basic Syntax `Outer template ${`Inner template ${variable}`} continues here` Simple Example Nested ...
Read MoreAwaiting on dynamic imports in JavaScript.
Dynamic imports in JavaScript allow you to load modules asynchronously at runtime using the import() function. This is particularly useful for code splitting and loading modules only when needed. Syntax const module = await import('./modulePath.js'); // or import('./modulePath.js').then(module => { // use module }); How Dynamic Imports Work The import() function returns a Promise that resolves to the module object. You can use await to handle it asynchronously: Dynamic Imports Example ...
Read MoreES6 Property Shorthands in JavaScript
ES6 introduced property shorthand syntax that simplifies object creation when the property name matches the variable name. Instead of writing name: name, you can simply write name. Syntax // ES5 way let obj = { property: property, anotherProperty: anotherProperty }; // ES6 shorthand let obj = { property, anotherProperty }; Basic Example ES6 Property Shorthands ...
Read MoreFirst class function in JavaScript
JavaScript treats functions as first-class citizens, meaning they can be stored in variables, passed as arguments, and returned from other functions. This powerful feature enables functional programming patterns and higher-order functions. What are First-Class Functions? In JavaScript, functions are first-class objects, which means they have all the capabilities of other objects. They can be: Stored in variables and data structures Passed as arguments to other functions Returned as values from functions Assigned properties and methods Storing Functions in Variables First-Class Functions // Store function ...
Read MoreAsynchronous Functions and the Node Event Loop in Javascript
Asynchronous functions allow programs to continue executing without waiting for time-consuming operations to complete. This non-blocking behavior is fundamental to JavaScript and Node.js, enabling better user experience and efficient resource utilization. When executing expensive operations like network requests or file I/O, asynchronous functions prevent the entire program from freezing. Instead of blocking execution, these operations run in the background while other code continues to execute. Understanding Asynchronous Execution In synchronous programming, each operation must complete before the next one begins. Asynchronous programming allows multiple operations to run concurrently, with callbacks or promises handling completion. console.log('One'); ...
Read MoreIs it possible to have JavaScript split() start at index 1?
The built-in String.prototype.split() method doesn't have a parameter to start splitting from a specific index. However, we can combine split() with array methods like slice() to achieve this functionality. Problem with Standard split() The standard split() method always starts from index 0 and splits the entire string: const text = "The quick brown fox jumped over the wall"; console.log(text.split(" ")); [ 'The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'wall' ] Method 1: Using split() with slice() The simplest ...
Read MoreSorting arrays by two criteria in JavaScript
When sorting arrays by multiple criteria in JavaScript, you need to create a custom comparator function that handles each condition sequentially. This article demonstrates sorting objects by priority (isImportant) first, then by date within each priority group. The Array Structure Consider an array of objects with id, date, and isImportant properties. We want important items first, then sort by date within each group: const array = [{ id: 545, date: 591020824000, isImportant: false, }, { id: 322, ...
Read More