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
Object Oriented Programming Articles
Page 109 of 589
Passing empty parameter to a method in JavaScript
In JavaScript, you can pass empty parameters to a method by simply omitting them when calling the function. This is useful when working with functions that have default parameters or when you want to rely on the function's default behavior. Understanding Default Parameters Default parameters allow functions to have fallback values when no arguments are provided or when undefined is passed. Passing Empty Parameters body { ...
Read MoreWhat are Partial functions in JavaScript?
Partial functions in JavaScript allow you to create specialized versions of functions by pre-filling some of their arguments. They take a function and some arguments, returning a new function that remembers those arguments and waits for the remaining ones. This technique is useful for creating reusable function variants and implementing functional programming patterns like currying. Basic Partial Function Example Partial Functions Partial Functions in JavaScript ...
Read MoreHow to get all unique values in a JavaScript array?
There are multiple ways to get unique values from a JavaScript array. The most common approaches use Set, filter(), or reduce() methods. Using Set (Recommended) The Set object only stores unique values, making it the simplest approach: Unique Values with Set let arr = [2, 3, 4, 2, 3, 4, "A", "A", "B", "B"]; console.log("Original array:", ...
Read MoreExplain import "as" and Export "as" constructs in JavaScript.
Import as and export as constructs allow you to rename modules during import/export operations, providing flexibility and avoiding naming conflicts in JavaScript. What is Import "as"? The import as syntax allows you to import a named export under a different name in the importing module. This is useful when you want to avoid naming conflicts or use more descriptive names. What is Export "as"? The export as syntax allows you to export a function or variable under a different name than its original declaration. This gives you control over how your module's API is exposed. ...
Read MoreSum of even numbers from n to m regardless if nm JavaScript
We are required to write a function that takes two numbers as arguments m and n, and it returns the sum of all even numbers that falls between m and n (both inclusive). For example: If m = 10 and n = -4 The output should be 10+8+6+4+2+0+(-2)+(-4) = 24 Approach We will first calculate the sum of all even numbers up to n and the sum of all even numbers up to m. Then we will check for the bigger of the two m and n. Subtract the sum of smaller ...
Read MoreBreaking a loop in functional programming JavaScript.
In traditional loops, we use break to exit early. In functional programming, JavaScript provides array methods like some() and every() that can terminate iteration based on conditions. Using Array.some() to Break Early The some() method stops iterating when the callback returns true, making it perfect for early termination: Breaking Loop Example body { font-family: ...
Read MoreContinuing a loop in functional programming JavaScript.
In functional programming JavaScript, there's no direct equivalent to the continue statement used in traditional loops. However, you can achieve similar behavior using array methods like filter(), forEach(), and some(). The Problem with Traditional Continue Traditional for loops use continue to skip iterations, but functional programming relies on array methods that don't support continue directly. Method 1: Using filter() and forEach() The most functional approach is to filter out unwanted elements first, then process the remaining items: Functional Loop Continue body { ...
Read MoreCreating 'Copy to Clipboard' feature on a web page with JavaScript
The copy to clipboard feature allows users to easily copy text from input fields or other elements on a webpage. This functionality is essential for improving user experience in web applications. Modern Approach Using Clipboard API The modern way to implement copy to clipboard uses the Clipboard API, which is more secure and reliable than the deprecated execCommand method. Copy to Clipboard body { ...
Read MoreThe generator.throw() method in JavaScript.
The generator.throw() method allows you to inject an error into a generator function at the current yield point. When called, it throws the specified error inside the generator and returns an object with done and value properties. Syntax generator.throw(exception) Parameters exception: The error object to be thrown inside the generator function. Basic Example function* simpleGenerator() { try { console.log("Before first yield"); yield 1; ...
Read MoreHow to reduce arrays in JavaScript?
The reduce() method transforms an array into a single value by applying a function to each element. It's commonly used for calculations like summing numbers, finding maximums, or combining data. Syntax array.reduce(callback(accumulator, currentValue, index, array), initialValue) Parameters: callback - Function executed on each element accumulator - Accumulated result from previous iterations currentValue - Current element being processed initialValue (optional) - Starting value for accumulator Example: Summing Array Elements ...
Read More