AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 434 of 840

Explain the finally Statement in JavaScript with examples.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

The finally statement in JavaScript always executes after try and catch blocks, regardless of whether an error occurred or not. This makes it ideal for cleanup operations and code that must run in all scenarios. Syntax try { // Code that may throw an error } catch (error) { // Handle error (optional) } finally { // Always executes } Example: Finally Block Always Executes ...

Read More

Implementation of Stack in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 540 Views

A stack is a Last In First Out (LIFO) data structure where elements are added and removed from the same end, called the top. This article demonstrates how to implement a stack in JavaScript using a class-based approach with basic operations like push, pop, and display. What is a Stack? A stack follows the LIFO principle - the last element added is the first one to be removed. Think of it like a stack of plates where you can only add or remove plates from the top. Item 3 ...

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

How to sum elements at the same index in array of arrays into a single array? JavaScript

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

We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array. If the original array is: [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ] Then the output should be: [60, 93, 30, 55] Let's explore different approaches to solve this problem. Using forEach() Method The most straightforward approach is to iterate through each sub-array and accumulate ...

Read More

What is JavaScript type coercion?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

Type coercion in JavaScript refers to the automatic conversion of values from one data type to another. This happens implicitly when JavaScript needs to perform operations between different types. How Type Coercion Works JavaScript automatically converts types in certain situations, such as when using operators or comparing values. This can sometimes lead to unexpected results if you're not familiar with the coercion rules. String Coercion Example Type Coercion Example JavaScript Type Coercion ...

Read More

Recursive sum all the digits of a number JavaScript

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

Let's say, we are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number. For example − findSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6 So, the output should be 6. How Recursive Digit Sum Works The process involves two levels of recursion: Extract and sum individual digits of a number Repeat the process until we get a single digit Method 1: Using Mathematical Operations ...

Read More

How to make a list of partial sums using forEach JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 347 Views

In JavaScript, creating a list of partial sums (also known as cumulative sums) means generating a new array where each element represents the sum of all elements up to that position in the original array. We have an array of numbers like this: const arr = [1, 1, 5, 2, -4, 6, 10]; We need to create a function that returns a new array where each element is the sum of all previous elements including itself: const output = [1, 2, 7, 9, 5, 11, 21]; Using forEach Method The ...

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 577 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

Window innerWidth and innerHeight Properties in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

The window.innerWidth and window.innerHeight properties return the width and height of the browser window's content area, excluding toolbars, scrollbars, and borders. Syntax let width = window.innerWidth; let height = window.innerHeight; Properties innerWidth - Returns the interior width of the window in pixels innerHeight - Returns the interior height of the window in pixels Both properties are read-only and return integer values Values change when the browser window is resized Example Window Dimensions ...

Read More

Shared properties in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 445 Views

In JavaScript, properties can be shared across all instances of an object by attaching them to the constructor function's prototype property. This creates a single shared property that all instances can access, rather than each instance having its own copy. How Prototype Properties Work When you access a property on an object, JavaScript first checks if the property exists on the instance itself. If not found, it looks up the prototype chain to find the property on the constructor's prototype. Student.prototype school: "St Marks" ...

Read More
Showing 4331–4340 of 8,392 articles
« Prev 1 432 433 434 435 436 840 Next »
Advertisements