Javascript Articles

Page 232 of 534

How to trim a file extension from a string using JavaScript?

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

Many web applications allow users to upload files and display filenames without extensions. Sometimes we need to store file content in databases using the filename without extension as a key. Here, we'll learn multiple ways to trim file extensions from strings using JavaScript. Using split(), pop() and join() Methods Every filename contains a file extension after the last dot. We can split the filename using '.' as a delimiter, remove the last element with pop(), and join the remaining parts back together. Syntax let split = fileName.split('.'); split.pop(); let finalName = split.join("."); Example ...

Read More

How to unpack array elements into separate variables using JavaScript?

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

Unpacking array elements (also called array destructuring) means assigning array element values to separate variables. We can assign every element to separate variables, or we can assign some elements while skipping others. In this tutorial, we will learn to unpack array elements into separate variables using JavaScript. Syntax Users can follow the syntax below to unpack the array elements into separate variables. let array = [element1, element2, element3, element4]; let [a, b, c, d] = array; In the above syntax, variable a contains the value of element1, b contains the value of element2, c ...

Read More

How to use the JavaScript function in the check box?

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

While working with HTML checkboxes, developers often need to execute JavaScript functions based on user interactions. This allows for dynamic content updates, form validation, and conditional display of elements based on checkbox selections. For example, you might have a checkbox asking "Are you above 18?" If users check this checkbox, you can show additional input fields like a PAN card number field. This requires calling JavaScript functions whenever users check or uncheck checkboxes. Using the onchange Event The onchange event attribute allows us to invoke a function whenever the user checks or unchecks a checkbox. This event ...

Read More

How to use map and filter simultaneously on an array using JavaScript?

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

The filter() method creates a new array with elements that pass a test, while map() transforms each element into something new. When used together, they provide a powerful way to both filter and transform data in a single chain. This combination is particularly useful when you need to process only certain elements of an array and then transform them. For example, filtering positive numbers and then calculating their square roots, or selecting employees with specific criteria and updating their salaries. Syntax of array.filter() method array.filter((element, index, self) => { // write a condition ...

Read More

How to use map() on an array in reverse order with JavaScript?

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

The map() method creates a new array by transforming each element. Sometimes you need to process array elements in reverse order. Here are three effective approaches to achieve this. Introduction to map() Method Syntax array.map((element, index, array) => { return element + 20; }) Parameters element – The current array element being processed index – The index of the current element array – The original array being mapped Method 1: Reverse Array Then Map First reverse the array using reverse(), then apply map() to the reversed ...

Read More

How to validate an input is alphanumeric or not using JavaScript?

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

Validating whether an input contains only alphanumeric characters (letters and numbers, no spaces or special characters) is a common requirement in JavaScript applications. An alphanumeric string contains only numeric digits (0-9) and alphabetical characters (A-Z, a-z). Below are two effective approaches to validate alphanumeric strings in JavaScript. Using the charCodeAt() Method The charCodeAt() method returns the ASCII value of a character. We can iterate through each character and check if its ASCII value falls within the valid ranges: 48-57: Numeric characters (0-9) 65-90: Uppercase letters (A-Z) 97-122: Lowercase letters (a-z) Syntax ...

Read More

How to validate email address using RegExp in JavaScript?

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

Anyone can make a mistake while entering the email in the input field. So, it's the developer's responsibility to check if users have entered a valid email string. Many libraries are available to validate email addresses, which we can use with various JavaScript frameworks, but not with vanilla JavaScript. However, if we want to use any library, we need to use its CDN. Here, we will use the regular expression to validate the email address in vanilla JavaScript. Basic Email Validation Pattern We have used the below regular expression pattern in our first example to validate the email. ...

Read More

How to zoom in and zoom out images using JavaScript?

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

The zoom-in and zoom-out functionality is essential for image interaction. By zooming in, users can examine image details closely, while zooming out provides a broader view. This feature is particularly useful for reading small text, viewing detailed graphics, or navigating large images. In this tutorial, we will learn to implement zoom functionality for images using JavaScript by manipulating CSS properties. Using Height and Width Properties The most straightforward approach is to modify the image's height and width properties. By increasing these values, we zoom in; by decreasing them, we zoom out. Syntax // Zoom ...

Read More

How to skip over an element in .map()?

Mohit Panchasara
Mohit Panchasara
Updated on 15-Mar-2026 17K+ Views

In JavaScript, we sometimes need to skip array elements while using the map() method. For example, we might want to map values from one array to another after performing mathematical operations only on finite values, or transform strings that meet specific criteria. Here are three effective approaches to skip over array elements while using the map() method. Using if-else Statement In the array.map() method, we can use an if-else statement to conditionally process elements. If an element meets our condition, we return the transformed value; otherwise, we return null or undefined. Syntax array.map((element) => ...

Read More

How to transform a JavaScript iterator into an array?

Mohit Panchasara
Mohit Panchasara
Updated on 15-Mar-2026 2K+ Views

In JavaScript, iterators are objects that implement the iterator protocol, allowing you to traverse through collections like Sets, Maps, or custom iterables. Unlike arrays, you cannot access iterator elements by index, so converting iterators to arrays is often necessary for easier manipulation. Here are three effective methods to transform JavaScript iterators into arrays. Using the for-of Loop The for-of loop iterates through each element of an iterator. Inside the loop, you can access elements and push them to an array using the push() method. Syntax for (let element of iterator) { ...

Read More
Showing 2311–2320 of 5,340 articles
« Prev 1 230 231 232 233 234 534 Next »
Advertisements