Object Oriented Programming Articles

Page 113 of 589

Fat arrow functions in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 382 Views

Fat arrow functions, introduced in ES6, provide a shorter syntax for writing functions in JavaScript. They use the => operator instead of the function keyword. Syntax // Basic syntax (param1, param2, ...) => { } // Single parameter (parentheses optional) param => { } // No parameters () => { } // Single expression (return implicit) (a, b) => a + b Basic Example Fat Arrow Functions ...

Read More

Sort the numbers so that the even numbers are ahead JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 268 Views

We have an array of numbers that contains some positive and negative even and odd numbers. We need to sort the array in ascending order but with a special requirement: all even numbers should appear before any odd number, and both groups should be sorted internally in ascending order. For example, if we have an array like [-2, 3, 6, -12, 9, 2, -4, -11, -8], the result should be [-12, -8, -4, -2, 2, 6, -11, 3, 9]. How It Works The solution uses a custom comparator function that: Checks if numbers are even ...

Read More

Fat vs concise arrow functions in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

Arrow functions in JavaScript come in two forms: fat arrow (with curly braces) and concise arrow (without curly braces). The concise form is a streamlined syntax for single-expression functions that provides implicit return functionality. Syntax Comparison Fat arrow function with explicit return: let add = (a, b) => { return a + b; } Concise arrow function with implicit return: let add = (a, b) => a + b; Single parameter (parentheses optional): let square = x => x * x; No parameters (parentheses required): ...

Read More

Pseudo mandatory parameters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 Views

Pseudo mandatory parameters in JavaScript allow you to enforce that certain function parameters must be provided by the caller. While JavaScript doesn't have built-in mandatory parameters like some other languages, you can simulate this behavior using various techniques. What are Pseudo Mandatory Parameters? Pseudo mandatory parameters are function parameters that appear optional syntactically but will throw an error if not provided. This helps catch bugs early and makes your function's requirements explicit. Method 1: Using a Helper Function The most common approach is to create a helper function that throws an error when called: ...

Read More

Can we check if a property is in an object with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 160 Views

JavaScript provides multiple ways to check if a property exists in an object. The most common approaches are using the in operator, hasOwnProperty() method, and Object.hasOwn() method. Using hasOwnProperty() Method The hasOwnProperty() method checks if the object has a specific property as its own (not inherited from the prototype chain). Property Check body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } ...

Read More

How to convert a node list to an array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 430 Views

A NodeList is returned by DOM methods like querySelectorAll() and getElementsByClassName(). While it's array-like, it doesn't have all array methods. Converting it to a true array gives you access to methods like map(), filter(), and forEach(). Using Array.from() (Recommended) The Array.from() method is the modern and most readable way to convert a NodeList to an array: Convert NodeList to Array body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: ...

Read More

TypeError: 'undefined' is not an object in JavaScript

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

The "TypeError: 'undefined' is not an object" error occurs when you try to access properties or call methods on an undefined variable. This error message is specific to Safari browser, while other browsers show similar but differently worded errors. What Causes This Error This error happens when: A variable is declared but not assigned a value An object property doesn't exist A function returns undefined and you try to access its properties Example: Accessing Property of Undefined Variable ...

Read More

Find and return array positions of multiple values JavaScript

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

We have to write a function, say findPositions() that takes in two arrays as argument. And it should return an array of the indices of all the elements of the second array present in the first array. For example − If the first array is ['john', 'doe', 'chris', 'snow', 'john', 'chris'], And the second array is ['john', 'chris'] Then the output should be − [0, 2, 4, 5] Therefore, let's write the code for this function. We will use a forEach() loop here. Example const values = ['michael', ...

Read More

How to sort strings with accented characters using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 471 Views

JavaScript's default string sorting doesn't handle accented characters properly. The localeCompare() method provides locale-aware string comparison that correctly sorts accented characters. The Problem with Default Sort Using the default sort() method on strings with accented characters produces incorrect results because it compares Unicode code points rather than the actual alphabetical order. Default Sort Problem Default Sort (Incorrect) ...

Read More

Combining multiple images into a single one using JavaScript

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

JavaScript's Canvas API allows you to combine multiple images into a single composite image. This technique is useful for creating overlays, watermarks, or blending effects in web applications. How Canvas Image Combining Works The process involves loading images into a canvas element and using the drawImage() method with different alpha transparency values to create layered effects. Example: Overlaying Two Images Combining Multiple Images body { ...

Read More
Showing 1121–1130 of 5,881 articles
« Prev 1 111 112 113 114 115 589 Next »
Advertisements