Shubham Vora

Shubham Vora

793 Articles Published

Articles by Shubham Vora

Page 6 of 80

Finding the time elapsed in JavaScript

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

Finding time elapsed in JavaScript is essential for performance monitoring, measuring execution time, and tracking user interactions. JavaScript provides several built-in methods to calculate time differences accurately. In this tutorial, we'll explore three different approaches: the Date() object for general time calculations, console.time() for debugging, and performance.now() for high-precision measurements. Using the Date() Object The Date() constructor returns the current timestamp in milliseconds since January 1, 1970 (Unix epoch). We can calculate elapsed time by finding the difference between two timestamps. Syntax let startTime = new Date(); let endTime = new Date(); let timeElapsed ...

Read More

How to create an id using mongoose in Javascript?

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

In this tutorial, we will learn to create an id using mongoose in JavaScript. Users can use the Mongoose NPM package in NodeJS to use MongoDB with NodeJS or connect MongoDB with the application. While storing the data in the MongoDB database, we must add a unique id to every data collection. However, if we don't add an id, it generates automatically and adds to the data. Syntax Users can follow the syntax below to create an id using mongoose in JavaScript. var newId = new mongoose.mongo.ObjectId(); In the above syntax, we access ...

Read More

How to stop browser's back button using JavaScript?

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

Stopping the browser's back button means preventing users from navigating to the previous page. Sometimes, we need to prevent users from going back from the current page for security purposes. For example, most banking sites don't allow you to go back when you are doing transactions through online banking. If users go back mid-transaction, it can create issues. So, they only allow you to either complete the transaction or cancel it and start again. Here, we will learn various approaches to prevent users from going back to the previous web page using JavaScript. Using window.history.forward() Method ...

Read More

How to store a key => value array in JavaScript?

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

In JavaScript, storing data in key-value pairs is essential for organizing information like user details, configuration settings, or any structured data. JavaScript provides several approaches to achieve this functionality. We can use different data structures, such as objects or maps in JavaScript, to store data in the key-value format. Using Objects to Store Key-Value Pairs In JavaScript, objects are the most common way to store data in key-value format. Objects allow dynamic property assignment and easy data retrieval using keys. Syntax let object = {}; object[key] = value; // or object.key = value; ...

Read More

How to store all dates in an array present in between given two dates in JavaScript?

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

Sometimes, we require to get all the dates between the given date range. In this tutorial, we will take two dates and find all dates between two dates. Also, we will store all dates in the array. Here, we will learn three approaches to storing all dates in an array present between given two dates in JavaScript. Using the while loop and setDate() method We can use the while loop for iteration and the setDate() method to set dates in the date object. On every iteration of the while loop, we can increase the date by one ...

Read More

How to swap the key and value of JSON element using JavaScript?

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

Here, we will learn to swap the key and value of JSON elements using JavaScript. In JavaScript, an object stores key-value pairs, and we can swap the key and value just like swapping two normal variables. In this tutorial, we will learn different approaches to swapping all the key-values of JSON elements using JavaScript. Using the for...in Loop We can iterate through the keys of the JSON object using a for...in loop. After that, we can access the value from the object using the key and swap every key-value pair in the object. We need to ...

Read More

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
Showing 51–60 of 793 articles
« Prev 1 4 5 6 7 8 80 Next »
Advertisements