AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 463 of 840

How can I get seconds since epoch in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 441 Views

To get seconds since epoch in JavaScript, you can use Date.getTime() which returns milliseconds since January 1, 1970 (Unix epoch), then divide by 1000 to convert to seconds. Syntax var date = new Date(); var epochSeconds = Math.round(date.getTime() / 1000); Method 1: Using Math.round() The most common approach uses Math.round() to handle decimal precision: var currentDate = new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log("Seconds since epoch:", epochSeconds); console.log("Type:", typeof epochSeconds); Seconds since epoch: 1594821507 Type: number Method 2: Using Math.floor() For exact truncation without ...

Read More

If ([] == false) is true, why does ([] || true) result in []? - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 604 Views

If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following − In the first case, we are using loose conditional checking, allowing type coercion to take over. While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hood. Let's now unveil the conversions that happen behind the scenes in both cases. Case 1 - ([] == false) According to the MDN docs, when two data ...

Read More

Validating email and password - JavaScript

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

Suppose, we have this dummy array that contains the login info of two of many users of a social networking platform − const array = [{ email: 'usman@gmail.com', password: '123' }, { email: 'ali@gmail.com', password: '123' }]; We are required to write a JavaScript function that takes in an email string and a password string. The function should return a boolean based on the fact whether or not the user exists in the database. Example Following is the code − ...

Read More

How to toggle between adding and removing a class name from an element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

JavaScript provides several methods to toggle class names on HTML elements. The most efficient approach is using the classList.toggle() method, which automatically adds a class if it doesn't exist or removes it if it does. Using classList.toggle() Method The toggle() method is the simplest way to switch between adding and removing a class: .highlight { background-color: #3498db; ...

Read More

In JavaScript, need to perform sum of dynamic array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

Let's say, we have an array that contains the score of some players in different sports. The scores are represented like this − const scores = [ {sport: 'cricket', aman: 54, vishal: 65, jay: 43, hardik: 88, karan:23}, {sport: 'soccer', aman: 14, vishal: 75, jay: 41, hardik: 13, karan:73}, {sport: 'hockey', aman: 43, vishal: 35, jay: 53, hardik: 43, karan:29}, {sport: 'volleyball', aman: 76, vishal: 22, jay: 36, hardik: 24, karan:47}, {sport: 'baseball', aman: 87, vishal: 57, jay: 48, hardik: 69, karan:37}, ]; We need to ...

Read More

How to print all students name having percentage more than 70% in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 617 Views

You can filter and display students with percentage more than 70% using various JavaScript approaches. This is useful for grade-based filtering and reporting. Following are the records of each student: const studentDetails = [ { studentName: "John", percentage: 78 }, { studentName: "Sam", percentage: 68 }, ...

Read More

How to access variables declared in a function, from another function using JavaScript?

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

In JavaScript, variables declared inside a function are scoped to that function and cannot be directly accessed from outside. However, there are several ways to make these variables available to other functions or globally. Problem with Function Scope Variables declared inside a function are private to that function: function myFunction() { let localVar = "I'm inside the function"; } myFunction(); // console.log(localVar); // This would cause an error Using Constructor Functions with 'this' You can use constructor functions to expose internal variables as properties: const num ...

Read More

JavaScript function to accept a string and mirrors its alphabet

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

We need to write a function that accepts a string and mirrors its alphabet. This means each letter is replaced with its counterpart from the opposite end of the alphabet. If the input is 'abcd' The output should be 'zyxw' The function maps every character to the letter that is (26 - N) positions away from it, where N is the 1-based index of that alphabet (like 5 for 'e' and 10 for 'j'). How It Works We use the String.prototype.replace() method to match all English alphabets regardless of case. For each letter: ...

Read More

Remove elements from array in JavaScript using includes() and splice()?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 416 Views

The includes() method checks whether an array contains a specific element, while splice() is used to add or remove items from an array. Together, they can be used to remove multiple elements from an array efficiently. Syntax array.includes(searchElement) array.splice(start, deleteCount) How It Works The approach involves iterating through the array and using includes() to check if each element should be removed. When a match is found, splice() removes it, and the index is decremented to account for the array shift. Example deleteElementsFromArray = function(elements, ...values) { let elementRemoved ...

Read More

Rotating an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 619 Views

In JavaScript, rotating an array means moving elements from one position to another by a specified number of steps. This operation is useful for circular data structures and algorithmic problems. For example, if we have an array [12, 6, 43, 5, 7, 2, 5] and rotate it by 3 positions to the left, the result would be [5, 7, 2, 5, 12, 6, 43]. Method 1: Using Array Prototype Extension We can extend the Array prototype to add a rotation method: // Helper function to rotate by one position const rotateByOne = arr => { ...

Read More
Showing 4621–4630 of 8,392 articles
« Prev 1 461 462 463 464 465 840 Next »
Advertisements