Javascript Articles

Page 526 of 534

Is it possible to change values of the array when doing foreach() in javascript?

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 6K+ Views

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 More

What's the difference between window.location and document.location?

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 2K+ Views

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 More

Get div height with vanilla JavaScript

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 13K+ Views

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 More

How to remove the hash from window.location (URL) with JavaScript without page refresh?

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 4K+ Views

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 More

How to get image data url in JavaScript?

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 6K+ Views

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 More

Convert Image to Data URI with JavaScript

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 13K+ Views

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 More

How to do string interpolation in JavaScript?

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 18K+ Views

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 More

Different techniques for copying objects in JavaScript

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 429 Views

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 More

Difference between .extend() / .assign() and .merge() in Lodash library.

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 1K+ Views

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 More

What is the use of .stack property in JavaScript?

Abdul Rawoof
Abdul Rawoof
Updated on 15-Mar-2026 691 Views

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
Showing 5251–5260 of 5,340 articles
« Prev 1 524 525 526 527 528 534 Next »
Advertisements