
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is there any way to check if there is a null value in an object or array in JavaScript?
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 to the fact that a declared variable that has not yet been given a value is undefined rather than null. There are many ways to check whether a value is null or not in JavaScript. Let’s discuss few ways one by one.
Using Object.keys()in JavaScript
The Object.keys() is a method in JavaScript that returns an array of the keys of an object. It takes one parameter, which is the object whose keys are to be returned. The order of the keys in the array is based on how they were added to the object; newer properties will appear after older ones. Object.keys can also be used for arrays, since arrays are objects as well and each element in an array has its own key (index).
Syntax
Following is the syntax for the Object.keys() method.
Object.keys(object)
Example
In the following example we are running the script using Object.keys.
<!DOCTYPE html> <html> <body> <script> var data = [{ name: "AVATAR", Car: "AUDI", Bike: null, Location: null }, { name: "RAM", Car: "No", Bike: 'BULLET', Location: 'LA' }, ]; data.forEach(function(v, i) { if ( Object.keys(v).some(function(k) { return v[k] == null; }) ) document.write('Contains null value at: ', i + "<br>"); else document.write(' data right', i); }); </script> </body> </html>
When the script is executed, the event is triggered, allowing us to check the entire data set that was used in the script to determine whether the null present is present or not, and it will display null present in the first set of data and actual data in the second set of data right on the webpage based on our data.
Using JavaScript some()method
The some() method accepts a function as a parameter and tests, whether at least one element in the array passes the test implemented by the given function.
It returns true, when an element for which the provided function returns true; otherwise it returns false.
Syntax
Following is the syntax for some() method
array.some(function(value, index, arr), this)
Example
Considering the following example, where we are using the some() method to check whether null is present or not.
<!DOCTYPE html> <html> <body> <script> var arr = [ { x : "1", y : "2", z : "3" }, { x : "ram", y : "abc", z : "var" }, { x : "abc", y : "def", z : null } ]; function hasNull(element, index, array) { return element.x===null || element.y===null || element.z===null; } document.write( arr.some(hasNull) ); </script> </body> </html>
On running the above script, the web-browser will display the value "true" on the webpage as the event gets triggered and checks whether the given array consists of null values or not, and it will display "true" as the condition was matched and the given array consists of null values.
Using JavaScript include() Method
The JavaScript include() method is used to load an external script or file into the current document. This method allows you to add scripts, libraries, and other files from outside of the existing HTML page. It can be used with both inline and external scripts. The include() method will execute the code in the specified file before continuing with the rest of your program.
Syntax
Following is the syntax for include()
string.includes(searchvalue, start)
Example
Let’s look into the following example where we are using the include() method
<!DOCTYPE html> <html> <body> <script> var nameArray = ["Jack", "rose", "Sam"]; if (nameArray.includes(null) == true) { document.write("array contains null value"); } else { document.write("array does not contains null value", "<br>"); } var objectData = { name: null }; if (Object.values(objectData).includes(null)) { document.write("object contains null value", "<br>"); } else { document.write("object does not contains null value", "<br>"); } </script> </body> </html>
When the script gets executed, the event gets triggered, which checks the data entered in the script to see whether, it contains null value or not and displays the value on the webpage. In the above case, it shows that the array does not contain null value and the object contains null value.
- Related Articles
- Is there a way to print all methods of an object in JavaScript?
- Is there is a standard function to check for null, undefined, or blank variables in JavaScript?
- Is there a “null coalescing” operator in JavaScript?
- Is there any way to skip some documents in MongoDB?
- Is there any way in MongoDB to get the inner value of json data?
- Is there any way to embed a PDF file into an HTML5 page?
- Is there any Python object inspector?
- Is there any way to use values from a JSON object in a MySQL Select statement?\n
- Is there any way to load an extension in chrome browser using Selenium Webdriver?
- Is there any more efficient way to code this “2 Sum” Questions JavaScript
- Is there any way to see the MongoDB results in a better format?
- Program to check there is any common reachable node in a graph or not in Python
- How to check if any value is Null in a MySQL table single row?
- Is there an elegant way of passing an object of parameters into a function?
- Is there a way to select a value which matches partially in MySQL?
