Articles on Trending Technologies

Technical articles with clear explanations and examples

How to create an ordered array from values that have an order number in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

When working with arrays containing values and order numbers, you often need to sort them by their order and extract just the values. This is common when dealing with image galleries, menu items, or any data that needs custom ordering. Problem Setup Let's say we have an array of strings where each string contains a file path and an order number separated by a comma: const images = [ 'photo1.jpg, 0', 'photo2.jpg, 2', 'photo3.jpg, 1' ]; console.log("Original array:", images); Original array: [ 'photo1.jpg, 0', 'photo2.jpg, ...

Read More

JavaScript get the length of select options(dropdown)?

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

In JavaScript, you can get the length of select options (dropdown) using different methods. This is useful for validation, dynamic content management, or determining how many options are available. HTML Structure Let's start with a basic select element: John Mike Sam David Carol Using Vanilla JavaScript The most straightforward approach uses the options.length property: ...

Read More

Add object to array in JavaScript if name does not already exist?

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

To add objects to an array only if they don't already exist, you can use push() combined with find() to check for duplicates. This prevents adding duplicate entries based on specific criteria. Basic Example Here's how to add objects to an array if the name property doesn't already exist: var details = [{name: "John"}, {name: "David"}]; var addObject = ["Mike", "Sam", "John"]; // "John" already exists addObject.forEach(obj1 => { if (!details.find(obj2 => obj2.name === obj1)) { details.push({name: obj1}); } ...

Read More

Reverse word starting with particular characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 278 Views

We need to write a JavaScript function that takes a sentence string and a character, then reverses all words starting with that specific character. For example, if we have the string: const str = 'hello world, how are you'; And we want to reverse words starting with 'h', the output should be: const output = 'olleh world, woh are you'; Notice that "hello" becomes "olleh" and "how" becomes "woh" because both start with 'h'. Solution We'll split the string into words, check each word's first character, and reverse those ...

Read More

How to count the number of occurrences of a character in a string in JavaScript?

varun
varun
Updated on 15-Mar-2026 897 Views

There are several methods to count character occurrences in a JavaScript string. Here are the most effective approaches: Using a For Loop The traditional method uses a simple for loop to iterate through each character: function countCharacter(str, char) { let count = 0; for (let i = 0; i < str.length; i++) { ...

Read More

How to return a string value version of the current number?

Manikanth Mani
Manikanth Mani
Updated on 15-Mar-2026 298 Views

The toLocaleString() method returns a string representation of a number formatted according to the user's locale (region and language settings). This method is useful for displaying numbers in a format familiar to users from different countries. Syntax number.toLocaleString() number.toLocaleString(locales) number.toLocaleString(locales, options) Parameters locales (optional): A string or array of strings representing locale identifiers (e.g., 'en-US', 'de-DE') options (optional): An object with formatting options like currency, minimumFractionDigits, etc. Basic Example Here's how to use toLocaleString() with different number formats: JavaScript toLocaleString() Method ...

Read More

Create a syntax highlighting code with JavaScript.

Srinivas Gorla
Srinivas Gorla
Updated on 15-Mar-2026 244 Views

Creating syntax highlighting for code blocks enhances readability and user experience. JavaScript offers several libraries for this purpose, with Google's Prettify being one of the simplest options. Using Google Prettify Library Prettify is a lightweight JavaScript library that automatically highlights code syntax. Include it in your HTML document: Basic Implementation Add the prettyprint class to any or element: function greetUser(name) { return "Hello, " + name + "!"; } ...

Read More

Is it possible to validate the size and type of input=file in HTML5?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 422 Views

Yes, it is possible to validate the size and type of input type="file" in HTML5. You can achieve this using JavaScript to access the File API and check file properties before form submission. HTML5 File Validation Structure The HTML5 File API provides access to file properties like size, type, and name through the files property of the input element. ...

Read More

Usage of margin-top property with CSS

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

The margin-top property specifies the top margin of an element in CSS. It creates space above an element, pushing it away from adjacent elements or the container's edge. Syntax margin-top: value; Values Length: Fixed values like 10px, 2em, 1rem Percentage: Relative to parent's width (e.g., 10%) auto: Browser calculates automatically inherit: Inherits from parent element Example: Fixed Values ...

Read More

How to know whether a value is searched in Javascript sets?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 246 Views

In JavaScript, sets are collections of unique values. Sometimes you need to check whether a specific element exists inside a set. In this article, we will explore different techniques to verify element presence in JavaScript sets. Using for Loop (Manual Search) You can search for an element by iterating through the set and comparing each element with the target value. If found, return true; otherwise, return false after checking all elements. HTML Console Output Console Output: ...

Read More
Showing 17201–17210 of 61,297 articles
Advertisements