Articles on Trending Technologies

Technical articles with clear explanations and examples

JavaScript to check consecutive numbers in array?

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

To check for consecutive numbers in an array, you can use JavaScript's reduce() method or simpler approaches. This returns true for consecutive sequences like 100, 101, 102, and false otherwise. Method 1: Using reduce() with Objects This approach works with arrays of objects containing number properties: const sequenceIsConsecutive = (obj) => Boolean(obj.reduce((output, latest) => (output ? (Number(output.number) + 1 === Number(latest.number) ? latest : false) : false))); console.log("Is Consecutive = " + sequenceIsConsecutive([ ...

Read More

How to set border width, border style, and border color in one declaration with JavaScript?

Paul Richard
Paul Richard
Updated on 15-Mar-2026 1K+ Views

To set the border width, style, and color in a single declaration, use the border property in JavaScript. This property accepts a shorthand value that combines all three border properties. Syntax element.style.border = "width style color"; The order is: width, style, color. You can also specify individual sides using borderTop, borderRight, borderBottom, or borderLeft. Example: Setting Border with Button Click Set Border Demo Text ...

Read More

Usage of font-style property in CSS

Giri Raju
Giri Raju
Updated on 15-Mar-2026 108 Views

The font-style property in CSS is used to specify whether text should be displayed in normal, italic, or oblique style. This property allows you to add emphasis to text by changing its visual appearance. Syntax font-style: normal | italic | oblique; Values normal - Default value. Text is displayed normally italic - Text is displayed in italic style (slanted) oblique - Text is displayed in oblique style (artificially slanted) Example: Basic Font Style Usage ...

Read More

JavaScript Sleep() function?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 53K+ Views

JavaScript doesn't have a built-in sleep() function like other languages (C's sleep(), Java's Thread.sleep(), Python's time.sleep()). However, we can create one using Promises and async/await. Creating a Sleep Function We can implement a sleep function using setTimeout() wrapped in a Promise: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } Using Sleep with Promises function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } console.log("Start"); sleep(2000).then(() => { console.log("After 2 seconds"); }); Start ...

Read More

Get the item that appears the most times in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 433 Views

Let's say we are required to write a function that takes in an array of string/number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequency map, and from that map we will return the index that makes the most appearances. Example const arr1 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34, 65, 34, 22, 67, 34]; const arr2 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34]; const mostAppearances = (arr) => { ...

Read More

How to format JSON string in JavaScript?

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

To format JSON string in JavaScript, use JSON.stringify() with spacing parameters. This method converts JavaScript objects to formatted JSON strings with proper indentation for better readability. Syntax JSON.stringify(value, replacer, space) Parameters value - The JavaScript object to convert replacer - Function or array to filter properties (use null for all properties) space - Number of spaces or string for indentation Example var details = { studentId: 101, studentFirstName: 'David', studentLastName: 'Miller', studentAge: 21, subjectDetails: { ...

Read More

How to calculate the nth root of a number in JavaScript?

Saurabh Jaiswal
Saurabh Jaiswal
Updated on 15-Mar-2026 1K+ Views

In JavaScript, calculating the nth root of a number requires understanding the mathematical rules for roots and applying appropriate methods. We'll explore two primary approaches: using Math.pow() and using logarithms. Understanding nth Root Rules Before calculating nth roots, it's important to understand when solutions exist: If the number is positive and the root is even, there are two solutions (positive and negative). Example: 2nd root of 16 is +4 and -4 If the number is positive and the root is odd, there is one positive solution. Example: 3rd root of 27 is 3 If the number ...

Read More

How to set the size of the background image with JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 6K+ Views

In this tutorial, we will look at the method to set the size of a background image with JavaScript. Background effects on a webpage enhance its look. We can set a background color or a background image to enhance the feel of the webpage. We can also change the background properties, like its size or color. To enhance the feel, the image must fit in the right position with the right size. Generally, we do this by using CSS, but JavaScript DOM also provides properties to do so. So, let us look at how to set the ...

Read More

How to delete Web Storage?

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

Web Storage provides methods to delete stored data from both Local Storage and Session Storage. Understanding how to properly clear this data is essential for maintaining user privacy and managing storage space effectively. Removing Individual Items To delete a specific item from storage, use the removeItem() method with the key name: // Store some data first localStorage.setItem('username', 'john'); localStorage.setItem('theme', 'dark'); ...

Read More

Set the font variant with CSS

Ankitha Reddy
Ankitha Reddy
Updated on 15-Mar-2026 132 Views

The CSS font-variant property controls how text is displayed, allowing you to transform lowercase letters into small capital letters or display text normally. Syntax font-variant: normal | small-caps | inherit; Parameters Value Description normal Default value. Text displays normally small-caps Converts lowercase letters to small capital letters inherit Inherits the font-variant from parent element Example: Using small-caps .small-caps { ...

Read More
Showing 18741–18750 of 61,297 articles
Advertisements