Javascript Articles

Page 98 of 534

How to allocate memory to an object whose length is set to 0 - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 131 Views

In JavaScript, you can manipulate array length to clear memory and allocate new slots. When you set an array's length to 0, it clears all elements. Setting length to a larger value allocates empty slots. Understanding Array Length Property The length property controls how many elements an array can hold. Setting it to 0 removes all elements, while setting it to a larger number creates empty slots filled with undefined. var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original ...

Read More

Fetch specific values from array of objects in JavaScript?

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

Fetching specific values from an array of objects is a common JavaScript task. This article demonstrates multiple approaches to filter and extract data based on specific criteria. Sample Data Let's start with an array of employee objects: const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" ...

Read More

Enter values with prompt and evaluate on the basis of conditions in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 290 Views

JavaScript's prompt() function allows you to collect user input and evaluate it using conditional statements. This is useful for creating interactive web applications that respond based on user-provided values. Basic Syntax To get user input and convert it to a number: var value = parseInt(prompt("Enter a value")); Example: Conditional Evaluation Based on User Input Here's a complete example that prompts for two values and evaluates their product: Prompt and Evaluate ...

Read More

Remove/ filter duplicate records from array - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 304 Views

JavaScript arrays often contain duplicate records that need to be removed. There are several effective methods to filter duplicates, depending on whether you're working with primitive values or objects. Sample Data with Duplicates Let's start with an array of nationality objects containing duplicate values: var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; console.log("Original array:", objectOfNationality); ...

Read More

Number prime test in JavaScript by creating a custom function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 Views

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can create a custom function to check if a number is prime. Basic Prime Number Function Here's a simple approach to check if a number is prime: function checkNumberIsPrime(number) { if (number

Read More

Comparing adjacent element and swap - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 455 Views

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This process repeats until the array is sorted. How Bubble Sort Works The algorithm repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Each pass "bubbles" the largest element to its correct position. Bubble Sort Process Initial: 10 30 5 ...

Read More

Checking a Checkbox with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 606 Views

In JavaScript, you can programmatically check a checkbox by setting its checked property to true. This is useful for form validation, user interactions, or initializing form states. HTML Structure First, let's look at a basic checkbox structure: John David Using the checked Property The checked property is a boolean that determines whether a checkbox is selected. Set it to true to check the checkbox, or false to uncheck it. Checkbox Example ...

Read More

Difference between two times using Dayjs JavaScript library?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 549 Views

Day.js is a lightweight JavaScript library for date manipulation. The diff() method calculates the difference between two time instances and returns the result in the specified unit. Syntax dayjs().diff(date, unit) Parameters date - The date to compare with unit - The unit of measurement: 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years' Example: Basic Time Difference Day.js Time Difference ...

Read More

What is the best way to reduce and merge a collection of objects – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 703 Views

The best way to reduce and merge a collection of objects is to use Object.values() combined with reduce() to group objects by a key and merge their properties. This approach is useful when you have duplicate objects and want to consolidate them, such as combining student records with the same ID. Example Data Consider this collection of student objects with duplicates: var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: ...

Read More

How to convert a string with zeros to number in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

When you have a string containing numbers with dots or zeros as separators, you can convert it to a number using JavaScript's built-in methods. This is commonly needed when processing formatted numeric data. The Problem Consider a string like "453.000.00.00.0000" where dots are used as thousand separators rather than decimal points. Direct conversion methods like Number() won't work correctly because JavaScript interprets the first dot as a decimal separator. let stringValue = "453.000.00.00.0000"; console.log("Original string:", stringValue); console.log("Direct Number() conversion:", Number(stringValue)); // NaN Original string: 453.000.00.00.0000 Direct Number() conversion: NaN Using parseInt() ...

Read More
Showing 971–980 of 5,340 articles
« Prev 1 96 97 98 99 100 534 Next »
Advertisements