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 526 of 534
Is it possible to change values of the array when doing foreach() in javascript?
Yes, it is possible to change array values during forEach() iteration in JavaScript. Unlike some programming languages where modifying arrays during iteration is restricted, JavaScript's forEach() allows you to modify the original array by using the array parameter or accessing the array directly by index. The forEach Method The forEach() method executes a provided function once for each array element. It passes three parameters to the callback function: the current element value, the element's index, and the array itself. You can use the array parameter to modify elements during iteration. Syntax array.forEach(function(value, index, array) { ...
Read MoreWhat's the difference between window.location and document.location?
In JavaScript, both window.location and document.location provide access to the current page's URL information, but they have subtle differences in browser compatibility and usage patterns. The window.location Property The window.location property is a reference to a Location object that represents the current URL of the document being displayed in that window. Since the window object is at the top of the scope chain, you can access location properties without the window prefix (e.g., location.href instead of window.location.href). Example: Getting Current URL JavaScript Get Current URL ...
Read MoreGet div height with vanilla JavaScript
The HTML element is a container used to group and style other HTML elements. Getting the height of a div element is a common task in JavaScript, and there are several properties available to achieve this, each measuring different aspects of the element's dimensions. Using offsetHeight The offsetHeight property returns the total height of an element including content, padding, border, and scrollbar (if present). Syntax element.offsetHeight The offsetHeight includes: offsetHeight = content height + padding + border + scrollbar height ...
Read MoreHow to remove the hash from window.location (URL) with JavaScript without page refresh?
The replaceState() method replaces the current history entry with the state objects, title, and URL passed as method parameters. This method is useful when you want to update the current history entry's state object or URL in response to a user action. To remove the hash from a URL without triggering a page refresh, we can use the HTML5 History API's replaceState() method. This approach modifies the browser's address bar while keeping the user on the same page. Syntax The syntax for the replaceState() method is as follows: window.history.replaceState(stateObj, title, url) Parameters ...
Read MoreHow to get image data url in JavaScript?
To obtain an image data URL in JavaScript, you can use several approaches depending on your source. The most common methods involve the Canvas API's toDataURL() method and the FileReader API for uploaded files. Using Canvas toDataURL() Method The toDataURL() method converts canvas content into a base64-encoded data URL. By default, it creates a PNG image, but you can specify JPEG format and quality. canvas.toDataURL(); // PNG format (default) canvas.toDataURL('image/jpeg'); // ...
Read MoreConvert Image to Data URI with JavaScript
JavaScript provides several methods to convert images to Data URI (Base64) format. A Data URI is a string representation of an image that can be embedded directly in HTML or CSS. This tutorial covers two main approaches: using Canvas API for image URLs and FileReader API for local files. Method 1: Converting Image URL to Data URI Using Canvas This method uses the HTML5 Canvas API to convert an image from a URL to a Base64 Data URI string. Convert Image URL to Data URI ...
Read MoreHow to do string interpolation in JavaScript?
String interpolation in JavaScript is a process in which expressions and variables are embedded directly into strings. This is achieved using template literals (backticks) with placeholder syntax ${}, making code more readable and maintainable than traditional string concatenation. Template literals use backticks (`) instead of quotes and allow expressions to be embedded using ${expression} syntax. This enables dynamic content insertion, mathematical calculations, and complex expressions within strings. Syntax `string text ${expression} more text` The expression inside ${} can be variables, function calls, mathematical operations, or any valid JavaScript expression that returns a value. ...
Read MoreDifferent techniques for copying objects in JavaScript
In JavaScript, objects are collections of key-value pairs. The properties of the object are the keys and are denoted with strings. The value of the key is the value of the property of the given object. Types of Object Copying There are 2 types of copying objects: shallow copy and deep copy. Shallow Copy In shallow copy, an object is copied at the top level only. If the object contains nested objects, only the references are copied, not the actual nested objects. Example Following is an example of shallow copy using Object.assign(): ...
Read MoreDifference between .extend() / .assign() and .merge() in Lodash library.
The Lodash library is a JavaScript utility library that provides helpful functions for working with arrays, strings, objects, numbers, and more. This article compares three important object merging methods: Object.assign(), _.extend(), and _.merge(). The Object.assign() Method Object.assign() is a native JavaScript method that performs shallow copying of enumerable properties from source objects to a target object. It modifies the target object and returns it. Syntax Object.assign(target, source1, source2, ...) Example var employee = { emp_name: "Abdul Rawoof", company: "Tutorials Point", salary: 18000, ...
Read MoreWhat is the use of .stack property in JavaScript?
The .stack property in JavaScript provides detailed information about errors, including the error type, message, and call stack trace. It shows where the error occurred and the sequence of function calls that led to the error. The stack property is available on Error objects and contains a string representation of the stack trace. This is extremely useful for debugging as it shows the exact location and path of execution when an error occurs. Format of Error Stack The stack trace format typically displays: First line: Error type and message Subsequent lines: Function call stack with file ...
Read More