AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 437 of 840

Dot notation vs Bracket notation in JavaScript

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

The dot notation and bracket notation are both methods for accessing object properties in JavaScript. While dot notation is cleaner and more commonly used, bracket notation provides greater flexibility, especially when working with dynamic property names or property names that contain special characters. Syntax Here's the basic syntax for both notations: // Dot notation object.propertyName // Bracket notation object["propertyName"] object[variableName] Basic Example: Accessing Properties Dot vs Bracket Notation ...

Read More

Find duplicate element in a progression of first n terms JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 208 Views

When you have an array of first n natural numbers with one duplicate element, finding the duplicate efficiently is a common programming challenge. We'll explore two mathematical approaches that solve this in linear time. Method 1: Using Array.prototype.reduce() This approach uses a mathematical trick with the reduce function to find the duplicate in a single pass: const arr = [2, 3, 1, 2]; const duplicate = a => a.reduce((acc, val, ind) => val + acc - (ind + 1)) + a.length - 1; console.log(duplicate(arr)); 2 How the Reduce Method Works ...

Read More

Loop through array and edit string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 812 Views

Let's say, we have to write a function, say translate() that accepts a string as the first argument and any number of words after that. The string will actually contain n $ signs like this āˆ’ This $0 is more $1 just a $2. Then there will be 3 strings which will replace the corresponding places. For example āˆ’ If the function call is like this: translate('This $0 is more $1 just a $2.', 'game', 'than', 'game'); The output of the function should be: This game is more ...

Read More

Concatenate two arrays of objects and remove repeated data from an attribute in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

When concatenating two arrays of objects, you often need to remove duplicates based on a specific attribute. This article demonstrates how to merge arrays while handling duplicate entries using map() and find() methods. The Challenge Consider two arrays of product objects where some products appear in both arrays with different properties. We want to concatenate them but prioritize data from the second array when duplicates exist. Example: Merging Product Arrays Let's merge two product arrays and remove duplicates based on productId: var details1 = [ { ...

Read More

Block Scoping in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

Block scope is an area between two curly braces { } which can be between loops, if conditions, or switch statements. The let and const keywords introduced in ES2015 allow us to create block-scoped variables that can only be accessed within that specific block. Understanding Block Scope Variables declared with let and const have block scope, meaning they exist only within the nearest enclosing block. This is different from var, which has function scope. Example: Block Scope with let Block Scoping Example ...

Read More

Setting object members in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 158 Views

In JavaScript, you can set object members (properties and methods) in several ways. This allows you to dynamically add or modify object properties after creation. Syntax // Dot notation object.propertyName = value; // Bracket notation object["propertyName"] = value; // Setting methods object.methodName = function() { // method body }; Example: Setting Object Properties Setting Object Members ...

Read More

Sorting objects according to days name JavaScript

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

Let's say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday. Sample Data Following is our array: const weather = [{ day: 'Wednesday', humidity: 60 }, { day: 'Saturday', humidity: 50 }, { ...

Read More

Is there any more efficient way to code this "2 Sum" Questions JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 325 Views

Our job is to write a function that solves the two-sum problem in at most linear time. Two Sum Problem Given an array of integers, we have to find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array. Brute Force Approach - O(n²) The naive approach uses nested loops to check every pair of elements: const bruteForceTwoSum ...

Read More

Get the first element of array in JavaScript

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

In JavaScript, there are several ways to get the first element of an array. The most common and efficient approaches include using bracket notation, the at() method, or destructuring assignment. Using Bracket Notation (Most Common) The simplest way is to access the element at index 0 using bracket notation: let fruits = ["apple", "banana", "orange"]; let firstFruit = fruits[0]; console.log(firstFruit); // Handle empty arrays let emptyArray = []; let firstElement = emptyArray[0]; console.log(firstElement); apple undefined Using the at() Method The at() method provides a modern alternative for accessing array ...

Read More

Explain JavaScript Error Object.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 370 Views

The Error object is thrown by the JavaScript interpreter when a script error occurs. It can also be used to create custom exceptions. Understanding Error objects helps in debugging and error handling in JavaScript applications. Error Object Properties Property Description name Sets or returns the name/type of the error message Sets or returns the error message as a string Example: Catching and Displaying Error Properties JavaScript Error Object body { ...

Read More
Showing 4361–4370 of 8,392 articles
« Prev 1 435 436 437 438 439 840 Next »
Advertisements