This tutorial will help a user to learn what is an object in JavaScript. How null is not an object though it show that null is an object.How function is an object.
An object in JavaScript is a set of key:value pairs. Every pair is called a property.
An object in JavaScript is any value that is NOT a {string, number (including NaN, Infinity) , true, false, null, undefined}.
Example:- Not a JavaScript object:
string:-
<script>
var p = "xyz";
document.write(typeof(p)); // output: string
</script>
number:-
<script>
var p = 99;
document.write(typeof(p)); // output: number
</script>
NaN:-
<script>
var p = 45;
document.write(isNaN(p)); // output: false
</script>
undefined:-
<script>
document.write(typeof(p)); // output: undefined
</script>
null:-
<script>
document.write(typeof(null)); // output: object
</script>
Though in case of null the output is object. But null is not a set of key:value pairs and we can not create or add properties to null.
<script>
null.p=99; // output: TypeError: null has no properties
</script>
Hence null is not an object.
Example:- JavaScript object: {p:6}; [7,99]; function fun{return Hello world}; Date;Regex;JSON;Math
object with a set of key value pairs:-
<script>
var p = {"X":99};
document.write(typeof(p)); // output: object
</script>
Array as an object:-
<script>
var p = [100,99];
document.write(typeof(p)); // output: object
</script>
Date as an object:-
<script>
var p = new Date();
document.write(typeof(p)); // output: object
</script>
Math as an object:-
<script>
var p = Math;
document.write(typeof(p)); // output: string
</script>
function as an object:-
<script>
var p = function yy(){
return "Hello World";
};
document.write(p); // output: function yy(){ return "Hello World"; }
</script>
function is an object as we can add property in the function
<script>
var p = function yy(){
return "Hello World";
};
p["p6"]=56;
document.write(p); // output: function yy(){ return "Hello World"; }
document.write(p["p6"]); //output: 56
</script>
0 Comment(s)