- JavaScript has typed values, not typed variables. In other programming languages, variables depicts the type of value stored in the variable, whereas JavaScript is a loosely typed language.
- JavaScript provides a built-in operator to check/ examine the value stored in the variable. This operator is termed as “typeof” operator.
- typeof operator always returns a String value.
- The returned string value of the typeof operator will always be one of the seven data types: string, boolean, number, undefined, object, symbol, function.
- Trick to Remember: typeof operator returns “object” , if the variable contains either array or null value.
- Examples:
var checkValue;
console.log(typeof checkValue); // “undefined”
checkValue = 22;
console.log(typeof checkValue); //”number”
checkValue = true;
console.log(typeof checkValue); //”boolean”
checkValue = “JavaScript”;
console.log(typeof checkValue); //”string”
checkValue = undefined;
console.log(typeof checkValue); //”undefined”
checkValue = { language: ‘JavaScript’};
console.log(typeof checkValue); //”object”
checkValue = Symbol(‘happy’);
console.log(typeof checkValue); //”symbol”
checkValue =[1,2,3,4];
console.log(typeof checkValue); //”object”
checkValue = null;
console.log(typeof checkValue); //”object”