Javascript Articles

Page 440 of 534

Sum of nested object values in Array using JavaScript

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

When working with complex nested data structures in JavaScript, you often need to sum values from deeply nested objects within arrays. This guide demonstrates various approaches to calculate the sum of nested object values efficiently. Understanding the Data Structure Consider a JSON object with nested arrays and objects where we need to sum the costNum values: let json = { storeData: [ { items: [ ...

Read More

De-structuring an object without one key

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 Views

Object destructuring with the rest operator (...) allows you to extract specific properties while collecting the remaining properties into a new object. This technique is useful when you want to exclude certain keys from an object. Syntax const { keyToExclude, ...remainingKeys } = originalObject; Example: Excluding One Key Object Destructuring body { ...

Read More

Can we modify built-in object prototypes in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

Yes, JavaScript allows modifying built-in object prototypes, but it should be done carefully. You can extend native objects like String, Array, or global functions like alert(). What are Built-in Object Prototypes? Built-in object prototypes are the foundation objects that JavaScript provides, such as String.prototype, Array.prototype, and global functions like alert(). Modifying them affects all instances of that type. Example: Overriding the alert() Function Custom Alert Function ...

Read More

Binding an object's method to a click handler in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

Binding an object's method to a click handler is essential when you need to maintain the correct this context within the method. This ensures the method can access the object's properties and other methods properly. Understanding the Context Problem When directly assigning an object method to an event handler, the this context gets lost and refers to the event target instead of the original object. Example Binding Object Methods to Click Handlers ...

Read More

Can we share a method between JavaScript objects in an array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

Yes, we can share methods between JavaScript objects in an array using prototypes or by defining shared methods outside the objects. This approach promotes code reusability and memory efficiency. Using Prototype Methods The most common way to share methods is through prototype inheritance. When you define a method on a constructor's prototype, all instances share that method. Shared Methods in JavaScript body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { ...

Read More

Referring JavaScript function from within itself

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 197 Views

In JavaScript, a function can call itself from within its own body, which is known as recursion. This technique is useful for tasks like calculating factorials, traversing nested structures, or repeating operations until a condition is met. What is Recursion? Recursion occurs when a function calls itself. Every recursive function needs two key elements: a base case (condition to stop) and a recursive case (function calling itself with modified parameters). Basic Example: Factorial Function Recursion Example ...

Read More

Parsing array of objects inside an object using maps or forEach using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 829 Views

When working with nested data structures in JavaScript, you often need to parse arrays of objects within objects. This can be efficiently done using map() or forEach() methods to iterate through the data and extract the information you need. Understanding the Data Structure Consider a JSON object containing store data with nested arrays of items: let json = { storeData: [ { items: [ ...

Read More

The new operator in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 481 Views

The new operator in JavaScript creates instances of user-defined objects or built-in object types that have constructor functions. When used with a constructor function, it creates a new object, sets up the prototype chain, and returns the newly created object. Syntax new constructor([arguments]) How the new Operator Works When you use the new operator, JavaScript performs these steps: Creates a new empty object Sets the object's prototype to the constructor's prototype Calls the constructor function with this pointing to the new object ...

Read More

Does JavaScript support block scope?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

JavaScript supports block scope for variables declared with let and const, but not for variables declared with var. Understanding this difference is crucial for writing predictable JavaScript code. What is Block Scope? Block scope means a variable is only accessible within the curly braces {} where it was declared. This includes if statements, for loops, while loops, and any other code blocks. Variables with let and const (Block Scoped) Variables declared with let and const are confined to their block: Block Scope with let/const ...

Read More

JavaScript code to print last element of an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 269 Views

Getting the last element of an array is a common operation in JavaScript. There are several methods to accomplish this, with different approaches depending on whether you want to modify the original array or not. Using Array Length (Recommended) The most efficient method uses the array's length property to access the last index: Last Array Element Array: ["A", 1, 2, 3, "B", "D"] Get Last Element ...

Read More
Showing 4391–4400 of 5,340 articles
« Prev 1 438 439 440 441 442 534 Next »
Advertisements