Javascript Articles

Page 346 of 534

How to convert a binary NodeJS Buffer to JavaScript ArrayBuffer?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 920 Views

Converting a binary NodeJS Buffer to JavaScript ArrayBuffer is a common requirement when working with binary data across different JavaScript environments. There are multiple approaches to achieve this conversion. Method 1: Using buf.buffer Property (Direct Access) The simplest approach is to access the buf.buffer property directly. However, this creates a view of the underlying ArrayBuffer, not a copy. const buffer = Buffer.from([1, 2, 3, 4, 5]); console.log("Original Buffer:", buffer); // Direct access to underlying ArrayBuffer const arrayBuffer = buffer.buffer; console.log("ArrayBuffer byteLength:", arrayBuffer.byteLength); // Create Uint8Array view to inspect data const uint8View = new Uint8Array(arrayBuffer); ...

Read More

Return Top two elements from array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

We have an array of numbers in JavaScript that contains numbers in an unsorted order. Our job is to write a function that takes in this array of numbers and returns an array of two elements, the top two elements of the array (greatest two elements of the array). We have to do this in one pass i.e., we need to execute this method in linear time like by using only one for loop or if we use ES6 function, we have to make sure to use only one and once and avoid nesting of methods which increases time ...

Read More

How to create SVG graphics using JavaScript?

Govinda Sai
Govinda Sai
Updated on 15-Mar-2026 721 Views

SVG (Scalable Vector Graphics) can be dynamically created and manipulated using JavaScript. This allows you to generate interactive graphics, charts, and animations programmatically. Creating SVG Elements with JavaScript To create SVG elements in JavaScript, use document.createElementNS() with the SVG namespace. Here's how to create a basic SVG container and add shapes: SVG with JavaScript // SVG namespace ...

Read More

How to display JavaScript variables in an HTML page without document.write?

Sravani S
Sravani S
Updated on 15-Mar-2026 564 Views

Use Element.innerHTML in JavaScript to display JavaScript variables in an HTML page without document.write(). This approach provides better control over content placement and doesn't overwrite the entire page. Basic Example: Displaying Variables Here's how to display JavaScript variables using innerHTML: Display Variables User Information Name: Age: City: // JavaScript variables ...

Read More

Reverse all the words of sentence JavaScript

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

We are required to write a JavaScript function that takes in a string and returns a new string which has all the words reversed from the original string. For example − If the original string is − "Hello World how is it outside" Then the output should be − "olleH dlroW woh si ti edistuo" Now, let's write the code for this function − Example const str = 'Hello World how is it outside'; const reverseSentence = str => { const arr = ...

Read More

How to secretly copy to clipboard JavaScript function in Chrome and Firefox?

Lokesh Badavath
Lokesh Badavath
Updated on 15-Mar-2026 1K+ Views

In JavaScript, you can programmatically copy text to the clipboard without user interaction using different methods. This tutorial shows how to implement "secret" clipboard copying that works silently in the background. We'll explore both the legacy execCommand() method and the modern Clipboard API for copying text to clipboard programmatically. Using execCommand() Method (Legacy) The execCommand() method was traditionally used for clipboard operations. Here's a basic implementation: Copy text ...

Read More

How to work with JavaScript Drag and drop for touch devices?

Lokesh Badavath
Lokesh Badavath
Updated on 15-Mar-2026 6K+ Views

JavaScript drag and drop functionality provides an intuitive way to move elements within web pages. While the HTML5 Drag and Drop API works well on desktop, touch devices require additional handling to ensure proper functionality. Basic Drag and Drop Concepts To make an element draggable in HTML5, add the draggable="true" attribute to the element. By default, images and text selections are draggable, but other elements need this attribute. Drag me Drag and Drop Events The drag and drop process involves two sets of events: Events on draggable elements: dragstart - Fires ...

Read More

How to fix Array indexOf() in JavaScript for Internet Explorer browsers?

Ramu Prasad
Ramu Prasad
Updated on 15-Mar-2026 505 Views

Internet Explorer versions 8 and below don't support the Array.indexOf() method. This article shows how to fix this compatibility issue using polyfills and jQuery alternatives. The Problem In modern browsers, indexOf() returns the first index of an element in an array, or -1 if not found. However, IE8 and earlier throw an error when you try to use this method. Method 1: Using Array Polyfill Add this polyfill to support indexOf() in older IE browsers: Method 2: Using jQuery.inArray() jQuery provides a cross-browser alternative with jQuery.inArray(): // Syntax: ...

Read More

Check for Ugly number in JavaScript

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

In the decimal number system, ugly numbers are positive integers whose only prime factors are 2, 3, or 5. This means an ugly number can be expressed as 2i × 3j × 5k where i, j, and k are non-negative integers. For example, the integers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 are all ugly numbers because they only contain the prime factors 2, 3, and 5. However, 7, 11, 13, 14 are not ugly numbers as they contain other prime factors. Algorithm To check if a number is ugly, we repeatedly divide it ...

Read More

How to convert the image into a base64 string using JavaScript?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 672 Views

To convert an image into a base64 string using JavaScript, you can use the FileReader API for local files or combine XMLHttpRequest with FileReader for remote images. Base64 encoding represents binary image data as a text string, making it useful for embedding images directly in HTML or CSS. Method 1: Converting Local Image Files For user-uploaded files, use the FileReader API with an input element: document.getElementById('imageInput').addEventListener('change', function(e) { ...

Read More
Showing 3451–3460 of 5,340 articles
« Prev 1 344 345 346 347 348 534 Next »
Advertisements