- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How many values does javascript have for nothing?
JavaScript has two values for nothing i.e., null and undefined. These two are also the primitive types in JavaScript.
Undefined
Out of the two values, Undefined in JavaScript means if a variable is declared and no value is assigned to the variable, then this variable is said to be undefined. An object can also be null. It is said to be null when there is no value to it.
Example 1
This example of the undefined value in JavaScript.
var str console.log('The value of given variable is:',str)
In the above example 1, a variable named ‘str’ is declared. So, as it is just declared and no value is assigned and by default it takes the value as ‘undefined’. When the value of the undefined variable is printed, it displays as “undefined” only.
Example 2
Following is another example of this value
var myVar if(myVar === void 0) { console.log("myVar has the value:",typeof(myVar)); }
Example 3
let a; console.log(a); function b() {} console.log(b())
NULL
Null in JavaScript is of type ‘Object’. The name itself refers that the value of the variable is nothing or it can be said that the value of the variable is absent (or zero). JavaScript cannot assign the value ‘null’ to any of the variables or objects unlike to that of undefined. The programmer or the user have to assign a variable or an object to null.
Example 1
This example demonstrates about the ‘null’ value in JavaScript −
var a = null; console.log("This value of given variable is:",a) console.log("The type of null value is:",typeof null)
In the above example, the variable is assigned a value null directly. When the value is printed in then it displays as ‘null’. And it also displays the type of null as ‘Object’.
As null means zero arithmetic operations can be performed when a variable is null and returns the result. But if a variable is undefined then no arithmetic operations can be performed and returns NaN (Not a Number).
Example 2
This example shows the operations that can be done for ‘undefined’ and ‘null’ values −
var a console.log("The variable is:",a,"Type of is:",typeof a) console.log("Value of undefined when addition is done:",a+1) var b = null console.log("The variable is:",b,"Type of is:",typeof b) console.log("Value of null when addition is done:",b+1)
Example 3
let a = null; function b() { return null } console.log(a); console.log(b())
- Related Articles
- How many sides does a cube have?
- How many bones does Girl body have?
- How Many Elements Does Periodic Table Have??
- How many bones does a baby have?
- How many states does a matter have?
- How many bones does an adult person have?
- How many poles does a cylindrical magnet have ?
- How many diagonals does a convex quadrilateral have?
- Does the eye have many colours?
- Does JavaScript have a standard for commenting functions?
- How many solutions does the linear equation $17x + 12y = 30$ have?
- Implement a MySQL Void Query that does nothing
- How many systems does a flowering plant have? Name and explain them.
- How many bones babies have?
- How many ribs do we have?
