Question: How to check data type in javascript
Answer:
All data type variables.
var booleanData = true; var intData = 95; var floatData = 1.55; var stringData = "String Data"; var symbolData = Symbol('Symbol Data Type'); var undefinedData; var objectData = {}; //object var newStringData = new String( "String Data using new keyword" ); //Object
checking data type with 'typeof' javascript operator.
console.log(typeof booleanData); console.log(typeof intData); console.log(typeof floatData); console.log(typeof stringData); console.log(typeof symbolData); console.log(typeof undefinedData); console.log(typeof objectData); console.log(typeof newStringData);
Output:
boolean number number string symbol undefined object object
null: JavaScript gave us "object" as the type for null, which is not correct.
var nullData = null; console.log(typeof nullData);
output:
object
We will use equality operator (==) or strict equality operator (===) to check null variable.
console.log(nullData == null); console.log(nullData === null);
output:
true true
We try to provide you the best content, if there is any mistake in this article or there is any mistake in code, then let us know.
Comments
Post a Comment