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 441 of 801
Can 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 MoreCreate HTML Document with Custom URL for the document in JavaScript
In JavaScript, you can create an HTML document with a custom URL using document.implementation.createHTMLDocument() and the element. This technique allows you to set a base URL that affects how relative URLs are resolved within the document. How It Works The createHTMLDocument() method creates a new HTML document. By adding a element to the document's head, you establish a base URL that all relative URLs in the document will resolve against. Example Custom Base URL Example ...
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 MoreHow to run functions iteratively with async await in JavaScript?
Running functions iteratively with async/await allows you to execute asynchronous operations in sequence within loops. This is useful when you need to process items one by one rather than concurrently. Basic Syntax async function iterativeFunction() { for (let i = 0; i < count; i++) { await asyncOperation(); } } Example: Sequential Function Calls async function test(i) { while (i { setTimeout(() => { ...
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 MoreMethod to check if array element contains a false value in JavaScript?
To check if an array element contains a false value in JavaScript, you can use methods like some(), includes(), or Object.values() depending on your data structure. Using some() for Simple Arrays The some() method tests whether at least one element passes a test function: const booleanArray = [true, false, true]; const hasfalseValue = booleanArray.some(value => value === false); console.log("Array contains false:", hasfalseValue); // Or more simply const hasFalse = booleanArray.includes(false); console.log("Using includes():", hasFalse); Array contains false: true Using includes(): true Using Object.values() for Nested Objects For complex nested structures, ...
Read MoreSelect and Deselect Text Inside an Element using JavaScript
In JavaScript, you can programmatically select and deselect text inside DOM elements using the Selection API. This is useful for creating interactive features like highlighting content or implementing custom text selection controls. Core Methods The key methods for text selection are: window.getSelection().selectAllChildren(element) - Selects all text inside an element window.getSelection().removeAllRanges() - Clears all current selections Example Select and Deselect Text body { ...
Read More