Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 457 of 801
Is having the first JavaScript parameter with default value possible?
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 MoreReverse a number in JavaScript
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 MoreIterating and printing a JSON with no initial key and multiple entries?
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 MoreCombine unique items of an array of arrays while summing values - JavaScript
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 MoreHow to delete all the DB2 packages in collection COLL1?
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 MoreFix Problem with .sort() method in JavaScript, two arrays sort instead of only one
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 MoreHow can I declare and define multiple variables in one statement with JavaScript?
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 MoreJavaScript : Why does % operator work on strings? - (Type Coercion)
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 MoreHow to match strings that aren't entirely digits in JavaScript?
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 MoreMerge object properties through unique field then print data - JavaScript
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