Web Development Articles

Page 457 of 801

Is having the first JavaScript parameter with default value possible?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

In JavaScript, you can have default values for the first parameter, but you need special techniques to skip it when calling the function. The most effective approach is using destructuring with spread syntax. The Challenge When you define a function with a default value for the first parameter, you cannot simply omit it in a regular function call: function multiply(firstParam = 10, secondParam) { return firstParam * secondParam; } // This won't work as expected: multiply(5); // firstParam = 5, secondParam = undefined Solution: Using Destructuring with Spread Syntax ...

Read More

Reverse a number in JavaScript

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

Our aim is to write a JavaScript function that takes in a number and returns its reversed number. For example, reverse of 678 is: 876 There are multiple approaches to reverse a number in JavaScript. Let's explore the most common methods. Method 1: Using String Conversion The most straightforward approach converts the number to a string, reverses it, and converts back to a number: const num = 124323; const reverse = (num) => parseInt(String(num) .split("") .reverse() .join(""), 10); console.log(reverse(num)); ...

Read More

Iterating and printing a JSON with no initial key and multiple entries?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 104 Views

When working with JSON arrays containing multiple objects, you can iterate through each entry using JavaScript's forEach() method. This is particularly useful when your JSON data doesn't have a single root key and contains multiple entries. Syntax array.forEach((element, index) => { // Process each element }); Example: Iterating Through Student Records const details = [ { "studentId": 101, "studentName": "John Doe" }, ...

Read More

Combine unique items of an array of arrays while summing values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 520 Views

We have an array of arrays, where each subarray contains exactly two elements: a string (person name) and an integer (value). Our goal is to combine subarrays with the same first element and sum their second elements. For example, this input array: const example = [ ['first', 12], ['second', 19], ['first', 7] ]; Should be converted to: [ ['first', 19], ['second', 19] ] Solution Using Object Mapping We'll create ...

Read More

How to delete all the DB2 packages in collection COLL1?

Mandalika
Mandalika
Updated on 15-Mar-2026 531 Views

A DB2 collection is a physical quantity which is used to group the packages. A collection can be simply termed as a group of DB2 packages. By using collections we can bind the same DBRM into different packages. In order to delete all the DB2 packages under a collection, we can issue the FREE PACKAGE command. Syntax FREE PACKAGE(collection_name.*) Example: Deleting All Packages in COLL1 To delete all packages in the collection named COLL1, use the following command: // DB2 command to delete all packages in COLL1 collection console.log("Executing: FREE PACKAGE(COLL1.*)"); ...

Read More

Fix Problem with .sort() method in JavaScript, two arrays sort instead of only one

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

One property of the Array.prototype.sort() function is that it is an in-place sorting algorithm, which means it does not create a new copy of the array to be sorted, it sorts the array without using any extra space, making it more efficient and performant. But this characteristic sometimes leads to an awkward situation. Let's understand this with an example. Assume, we have a names array with some string literals. We want to keep the order of this array intact and want another array containing the same elements as the names array but sorted alphabetically. We can do something ...

Read More

How can I declare and define multiple variables in one statement with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

In JavaScript, you can declare and define multiple variables in a single statement by separating them with commas. This approach works with var, let, and const keywords. Syntax var variable1 = value1, variable2 = value2, variable3 = value3; // Or with let/const let variable1 = value1, variable2 = value2, variable3 = value3; Example: Using var var firstName = "My First Name is David", lastName = "My Last Name is Miller", ...

Read More

JavaScript : Why does % operator work on strings? - (Type Coercion)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

Let's say we have a code snippet that produces some surprising results. First, the modulo operator works with strings (unexpectedly). Second, concatenation of two strings produces awkward results. We need to explain why JavaScript behaves this way through type coercion. Problem Code const numStr = '127'; const result = numStr % 5; const firstName = 'Armaan'; const lastName = 'Malik'; const fullName = firstName + + lastName; console.log('modulo result:', result); console.log('full name:', fullName); modulo result: 2 full name: ArmaanNaN What is Type Coercion? Type coercion is JavaScript's automatic conversion of ...

Read More

How to match strings that aren't entirely digits in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 115 Views

In JavaScript, you can use regular expressions to match strings that contain non-digit characters. This is useful when parsing complex data strings where you need to identify values that aren't purely numeric. Problem Statement Consider this complex string containing various data types: studentId:"100001",studentName:"John Smith",isTopper:true,uniqueId:10001J-10001,marks:78,newId:"4678" We want to extract values that contain characters other than digits, excluding boolean values and null. Using Regular Expression The following regular expression matches strings that aren't entirely digits: var regularExpression = /(?

Read More

Merge object properties through unique field then print data - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

Let's say we have a students object containing two properties names and marks. The names is an array of objects with each object having two properties name and roll, similarly marks is an array of objects with each object having properties mark and roll. Our task is to combine the marks and names properties according to the appropriate roll property of each object. Problem Statement The students object is given here: const students = { marks: [{ roll: 123, ...

Read More
Showing 4561–4570 of 8,010 articles
« Prev 1 455 456 457 458 459 801 Next »
Advertisements