In this article, we will see some basic data types in JavaScript with some examples.
Six data types that are primitives
1.undefined
2.Boolean
3.Number
4.String
5.BigInt(ECMA morden JavaScript)
6.Symbol(ECMA morden JavaScript)
Basic Data Types in JavaScript
1.undefined
The undefined property indicates that a variable has not been assigned a value.
Example:
var abcd;
console.log(abcd);
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
undefined
PS C:UsersThe-BeastDesktopjavascript Blog>
2.Boolean
In JavaScript Boolean represents true or false.
Exmaple:
var iAmPatel=true;
console.log(iAmPatel)
console.log(typeof(iAmPatel));
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
true
boolean
PS C:UsersThe-BeastDesktopjavascript Blog>
3.Number
In JavaScript numbers can be written with decimals or without decimals.
Example:
var myAge=22;
console.log(myAge);
console.log(typeof(myAge));
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
22
number
PS C:UsersThe-BeastDesktopjavascript Blog>
4.String
Strings are useful for holding data that can be represented in text form. A string is used to represent and manipulate a sequence of characters. In JavaScript string characters written inside quotes.
Example:
var myName="patel";
var myname='rocoderes'
console.log(myName);
console.log(myname)
console.log(typeof(myName));
console.log(typeof(myname));
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
patel
rocoderes
string
string
PS C:UsersThe-BeastDesktopjavascript Blog>
Now challenge time
10 + 20
9 - "5"
"java" + "Script"
" " + " "
" " + 0
"Ro" + "coderes"
true + true
true + false
false + true
false - true
solution:
console.log(10 + 20);
console.log( 9 - "5"); //bug
console.log("java" + "Script");
console.log( " " + " ");
console.log(" " + 0);
console.log("Ro" - "coderes");
console.log(true + true);
console.log(true + false);
console.log(false + true);
console.log(false - true);
output:
In javascript 1 represent is true Or 0 represent is false
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
30
4
javaScript
0
NaN
2
1
1
-1
PS C:UsersThe-BeastDesktopjavascript Blog>
1.interview question
Difference between null VS undefined?
- null means empty.
- when you create a variable but not assign any value. that is undefined
Example:
var iAmUseless=null
console.log(iAmUseless)
var iAmStandBy;
console.log(iAmStandBy)
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
null
undefined
PS C:UsersThe-BeastDesktopjavascript Blog>
2.interview question
what is NaN?
- NaN is a property of the global object.
- In other words, it is a variable in the global scope.
- The initial value of NaN is Not-A-Number.
Example:
console.log("Ro" - "coderes");
var myPhoneNumber=9876543210;
var myName="patel";
console.log(isNaN(myPhoneNumber));
console.log(isNaN(myName));
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
NaN
false
true
PS C:UsersThe-BeastDesktopjavascript Blog>