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
Javascript Articles
Page 437 of 534
Passing parameters to callback functions JavaScript
Callback functions in JavaScript can accept parameters just like regular functions. When passing a callback to another function, you can provide parameters that the callback will use when executed. Basic Callback with Parameters A callback function receives parameters when it's invoked by the calling function: Callback Parameters Run Example function add2(a) { ...
Read MoreFat arrow functions in JavaScript
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 MoreFat vs concise arrow functions in JavaScript
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 MorePseudo mandatory parameters in JavaScript
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 MoreCan we check if a property is in an object with JavaScript?
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 MoreHow to convert a node list to an array in JavaScript?
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 MoreTypeError: 'undefined' is not an object in JavaScript
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 MoreHow to sort strings with accented characters using JavaScript?
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 MoreCombining multiple images into a single one using JavaScript
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 MoreJavaScript code to de-select text on HTML page.
In JavaScript, you can deselect or clear text selections on an HTML page using the window.getSelection().removeAllRanges() method. This is useful for creating interactive interfaces where you want to programmatically clear user text selections. The Selection API The Selection API provides methods to work with text selections in the browser. The window.getSelection() object represents the current text selection, and removeAllRanges() clears all selected ranges. Syntax window.getSelection().removeAllRanges(); Example Deselect Text Example ...
Read More