Found 6683 Articles for Javascript

Get current date/time in seconds - JavaScript?

Yaswanth Varma
Updated on 21-Apr-2023 16:50:21

12K+ Views

In JavaScript, there are built-in methods for getting the current date and time in seconds. We are going to perform this in two ways − Using the Date.now() method Using a new date getTime() method Let’s dive into the article for getting better understanding on getting current date time in seconds. Using Date.now() method The Date.now() static method returns the milliseconds that have passed since the epoch, which is recognized as the beginning of January 1, 1970, UTC at midnight. Syntax Following is the syntax for Date.now() − Date.now() Example In the following example, we are running ... Read More

In JavaScript, what is meant by 'a function expression is always a constant value'?

AmitDiwan
Updated on 09-Nov-2020 08:27:33

68 Views

If the const is used in a program, and if you try to reassign the value to const variable then an error will arise.Let’s say the following is our const variable −const result = (first, second) => first * second;Now, we will try to reassign a value to the const variable and an erro can be seen in the output.ExampleFollowing is the code −const result = (first, second) => first * second; result = first => first =first*10; console.log(result(10, 20)); To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo284.js.OutputThis will produce ... Read More

How to remove all blank objects from an Object in JavaScript?

Yaswanth Varma
Updated on 21-Apr-2023 16:46:59

3K+ Views

The task at hand is to learn how to remove all blank objects from a JavaScript object. Let’s consider the following object − const details = { name: 'John', age: {}, marks: { marks: {} } } We need to remove the black objects above You can use forEach() along with typeof and delete to remove blank objects. Let’s dive into the article for getting better understanding on removing blank objects. Using reduce() A reducer function is run for each element of an array using the reduce() method. The function's ... Read More

How to count number of occurrences of repeated names in an array - JavaScript?

Yaswanth Varma
Updated on 21-Apr-2023 17:05:26

1K+ Views

An array in JavaScript is a group of identically typed elements that are stored in adjacent memory locations and may each be separately referred to using an index to a special identifier. Let’s consider an array consisting of occurrences of repeated names. The array looks like this − var details = [ { studentName: "John", studentAge: 23 }, { studentName: "David", studentAge: 24 }, ... Read More

Hide a video tag on a web page - JavaScript

AmitDiwan
Updated on 09-Nov-2020 08:18:01

3K+ Views

Let’s say we have the following sample video tag on a web page        You cannot play video here...... To hide a video on a web page, use yourVariableName.style.display=’none’.ExampleFollowing is the code −            Document    .hideVideo {       display: block;       z-index: 999;       margin-top: 10px;       margin-left: 10px;    }               You cannot play video here......        var hideVideo = document.getElementsByClassName("hideVideo")[0];    hideVideo.style.display = ... Read More

Make strings in array become keys in object in a new array in JavaScript?

Yaswanth Varma
Updated on 21-Apr-2023 17:23:05

1K+ Views

The task we are going to perform in this article is make strings in array become keys in object in a new array in JavaScript. Let’s consider a simple array − let array= ['bike', 'car'] Now, we are going to use map() to convert the above array to a new array (keys in an object). The array will then look like this − let newArray= [{shorts: []}, {tees: []}, ] Let’s dive into the article to learn more about on making a strings in array become keys in object in a new array in JavaScript. Using map() The ... Read More

Implement Bubble sort with negative and positive numbers – JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:13:49

699 Views

Let’s say the following is our unsorted array with negative and positive numbers −var arr = [10, -22, 54, 3, 4, 45, 6];ExampleFollowing is the code to implement Bubble Sort −function bubbleSort(numberArray, size) {    for (var lastIndex = size - 1; lastIndex > 0; lastIndex--) {       for (var i = 0; i < lastIndex; i++) {          if (numberArray[i] > numberArray[i + 1]) {             var temp = numberArray[i];             numberArray[i] = numberArray[i + 1];             numberArray[i + ... Read More

How to detect and replace all the array elements in a string using RegExp in JavaScript? 

Yaswanth Varma
Updated on 21-Apr-2023 17:24:34

989 Views

An array is an ordered list of values in JavaScript. The term "element designated by an index" refers to each value: The following traits describe a JavaScript array: An array can initially store values of many sorts. You could, for instance, store elements of the number, string, Boolean, and null types in an array. Let’s jump into the article to detect and replace all the array elements in a string using regexp in JavaScript. Using regexp in JavaScript A regular expression is a character pattern. Pattern-matching "search-and-replace" operations are performed on text using the pattern. A RegExp Object is a ... Read More

Substitute random items in a JavaScript array?

Yaswanth Varma
Updated on 21-Apr-2023 17:26:42

244 Views

To substitute random items with items from an array with different ones in JavaScript. Let’s look at the instance; the array is − Var array=[m, n, o, p] Let's choose two random items to be replaced with a, while the others remain in their original positions. If m and n are the randomly selected items, and one is removed on one instance, the array would be − Changed array=[a, a, o, p] Let’s dive into the article to learn more about substituting random items in a JavaScript array. To substitute random items, use random() along with map(). Using ... Read More

What happens when length of object is set to 0 - JavaScript?

AmitDiwan
Updated on 09-Nov-2020 08:07:12

258 Views

Let’s say the following is our array object −var arrayObject =    [       "John",       "David",       "Mike"    ]You can use length property to set the length to 0 and clear memoryThe syntax is as follows to clear memory −yourArrayObjectName.length=0; // To clear memory yourArrayObjectName.length=4; // To allocate memoryOutputThis will produce the following output on console −var arrayObject =    [       "John",       "David",       "Mike"    ] arrayObject.length = 0; console.log(arrayObject); arrayObject.length = 5; for (var i = 0; i < arrayObject.length; i++)   ... Read More

Advertisements