Object Oriented Programming Articles

Page 106 of 589

Order items alphabetically apart from certain words JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

In JavaScript, you can sort an array alphabetically while keeping certain priority words at the top. This is useful when you need specific items to appear first, regardless of alphabetical order. Let's create a function excludeSort(arr, ex) where arr is the array to be sorted and ex contains the priority words that should appear at the top. How It Works The sorting logic uses a custom comparator that: Places priority words (from ex array) at the top Sorts remaining words alphabetically Uses Array.includes() to check if a word is in the priority list Example ...

Read More

How to declare Block-Scoped Variables in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 Views

Block-scoped variables are declared using let and const keywords introduced in ES2015. Unlike var, these variables are only accessible within their containing block. Block scope means the variable exists only within the nearest enclosing curly braces {}. Syntax let variableName = value; const constantName = value; Example: Basic Block Scope Block Scoped Variables Block Scoped Variables Demo Test Block Scope ...

Read More

How to check existence of NaN keyword in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We have an array of elements that contains both truthy and falsy values. Our job is to write a function that returns an array with indices of those elements which are NaN in the original array. NaN !== NaN The datatype of NaN is actually number. Although NaN is a falsy value, it has a peculiar property that no other datatype or variable has. It's that the expression NaN === NaN yields false. And it's only in the case of NaN that this is false. So, we can use this behavior to our advantage and pick out ...

Read More

Object.fromEntries() method in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

The Object.fromEntries() method in JavaScript converts an iterable of key-value pairs (like arrays or Maps) into an object. It's the reverse operation of Object.entries(). Syntax Object.fromEntries(iterable) Parameters iterable: An iterable containing key-value pairs, such as an array of arrays or a Map. Return Value Returns a new object with properties derived from the key-value pairs in the iterable. Example: Converting Array of Arrays Object.fromEntries() Example Object.fromEntries() ...

Read More

JavaScript program to merge two objects into a single object and adds the values for same keys

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 579 Views

We have to write a function that takes in two objects, merges them into a single object, and adds the values for same keys. This has to be done in linear time and constant space, means using at most only one loop and merging the properties in the pre-existing objects and not creating any new variable. So, let's write the code for this function − Example const obj1 = { value1: 45, value2: 33, value3: 41, value4: 4, ...

Read More

Explain Optional Catch Binding in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 518 Views

The optional catch binding introduced in ES2019 allows us to omit the error parameter in catch blocks. Instead of catch(error), we can simply write catch when we don't need to access the error object. This feature is useful when we know the type of error in advance or want to handle errors without examining their details. Syntax Comparison Traditional catch binding requires parentheses and a parameter: try { // code that might throw } catch (error) { // handle error using 'error' parameter } Optional ...

Read More

Sum of consecutive numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 575 Views

Let's say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together. For example − const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2]; console.log("Original array:", array); Original array: [ 1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2 ] The output should be − [1, 15, 16, 9, 1, 8, 2] All consecutive 5s added up to 15, then 2 consecutive 8s added up to ...

Read More

Object de-structuring in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 169 Views

Object destructuring is a JavaScript feature that allows you to extract multiple properties from an object and assign them to variables in a single statement. It provides a clean and concise way to work with object properties. Syntax const { property1, property2, property3 } = object; Basic Example Object Destructuring Object Destructuring Example ...

Read More

Get global variable dynamically by name string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

In JavaScript, you can access global variables dynamically using their name as a string through the window object (in browsers) or global object (in Node.js). Syntax // Browser environment window[variableName] // Node.js environment global[variableName] // Using globalThis (works in both) globalThis[variableName] Basic Example Dynamic Global Variables // Define global variables ...

Read More

Get the longest and shortest string in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 793 Views

We have an array of string literals like this: const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; We need to write a function that returns the longest and shortest word from this array. We will use the Array.prototype.reduce() method to keep track of the longest and shortest word during iteration. Using Array.reduce() Method The reduce() method processes each element and maintains an accumulator object containing both the longest and shortest strings found so far: const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; const findWords = ...

Read More
Showing 1051–1060 of 5,881 articles
« Prev 1 104 105 106 107 108 589 Next »
Advertisements