Found 6683 Articles for Javascript

Is there any way to check if there is a null value in an object or array in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 11:27:34

8K+ Views

Null values can be tricky to work with in JavaScript, but they are an important part of the language. In this article, we will discuss various ways you can check if there is a null value in an object or array in JavaScript. The null values display that no object value is present. It is intentionally set to show that a variable has been declared but has not yet been given a value. The primitive value is undefined, which is an unintended absence of any object value, which is comparable to null, that contrasts with the former. This is due ... Read More

Shortest syntax to assign properties from function call to an existing object in JavaScript

Yaswanth Varma
Updated on 18-Jan-2023 11:33:30

76 Views

In JavaScript, objects can be created and assigned properties in a variety of ways. One popular approach is to use the shortest syntax to assign properties from a function call to an existing object. In this article, we will explore how to use this technique and discuss some considerations when using it. An object in JavaScript is a separate entity having properties and a type. JavaScript objects can have properties that specify their attributes. Spread syntax (…) An iterable, such as an array or string, can be expanded in places where zero or more arguments or components are ... Read More

Remove property for all objects in array in JavaScript?

Yaswanth Varma
Updated on 19-Jul-2023 15:12:53

850 Views

The task we are going to perform in this articles is to “Remove property for all objects in array in JavaScript”. A collection of key-value pairs constitutes an object in JavaScript. A key-value pair among them is referred to as an object property. Any data type, including Number, String, Array, Object, etc., can be used for both the keys and values of properties There are two methods: one is mutable and uses the delete operator; the other is immutable and uses object restructuring. Let’s discuss this method. The delete operator The delete operator eliminates both the property's value and the ... Read More

Array.prototype.fill() with object passes reference and not new instance in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:08:01

132 Views

To fix this, you can use map() in JavaScript.The syntax is as follows −var anyVariableName= new Array(yourSize).fill().map(Object);ExampleFollowing is the code −var arrayOfObject = new Array(5).fill().map(Object); console.log(arrayOfObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo311.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo311.js [ {}, {}, {}, {}, {} ]

How to make a setInterval() stop after some time or after a number of actions in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:06:41

1K+ Views

Use some conditions to stop after some time.The below code will stop in half a minute.ExampleFollowing is the code −            Document    var now = new Date().getTime();    var interval = setInterval(function () {       if (new Date().getTime() - now > 30000) {          clearInterval(interval);          return;       }       console.log("working");    }, 2000); To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option “Open with live server” in VS Code editor.OutputThis will produce the following output −

How to sort an array of integers correctly in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:04:21

313 Views

To sort an array of integers, use sort() in JavaScript.ExampleFollowing is the code −var arrayOfIntegers = [67, 45, 98, 52]; arrayOfIntegers.sort(function (first, second) {    return first - second; }); console.log(arrayOfIntegers);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo310.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo310.js [ 45, 52, 67, 98 ]

How to stop the loop for JavaScript scroll down?

Yaswanth Varma
Updated on 18-Jan-2023 11:17:48

598 Views

JavaScript scroll down loops can be extremely annoying and disruptive to a web page. Fortunately, it is relatively easy to stop the loop if you know what steps to take. In this article, we will discuss how to stop the JavaScript scroll down loop so that your website visitors don't have to keep scrolling endlessly. We'll also provide some tips on preventing these types of issues in the future. JavaScript method ScrollBy() scrolls the web page to the specific number of pixels. The clearInterval()in JavaScript The clearInterval() is a JavaScript function that is used to stop the execution of a ... Read More

JavaScript subtraction of two float values?

Yaswanth Varma
Updated on 18-Jan-2023 11:44:20

2K+ Views

As we are familiar with the subtraction of two float values and we know how to use the subtraction condition between float values. To correctly subtract two float values, use parseFloat() along with toFixed(). Let’s dive into the article to learn more about JavaScript subtraction of two float values. The parseFloat()function The string is taken as input and converted into a floating-point value using the parseFloat() method. The function returns NaN, or not a number, if the string is empty or if its first character is not a numeric value. Up until the point where it runs into ... Read More

Use jQuery to get values of selected checkboxes?

AmitDiwan
Updated on 26-Oct-2020 10:56:01

343 Views

Yes, we can use jQuery to get values of selected checkboxes using the concept of input. We have to also use the :checked selector.ExampleFollowing is the code −            Document        Cricket        Listening Music Reading NewsPaper Click Me    $(document).ready(function () {       $("button").click(function () {          $('input[name="checkDemo"]:checked').each(function () {             console.log(this.value);          });       });    }); To run the above program, save the ... Read More

Replace whitespace in different elements with the same class in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 12:04:10

350 Views

In this article we are going to learn about replace whitespace in different elements with same class in JavaScript. Let’s look into the article to learn more about replacing whitespaces in different elements. Using split() and join() Methods The split() method divides a string into several substrings and returns them as an array. When a separator is present in the string, the string is split according to the separator that was specified as a parameter. This parameter specifies the space character (" ") to divide the string whenever a space is present. A separator is used to join an array ... Read More

Advertisements