There are seven data types in JavaScript, six simple (primitive) and one complex.
Simple data types:
Complex data type:
- Object.
The typeof operator
JavaScript is loosely typed and variables are defined without declaring data type. To determine the data type, the typeof operator should be used. The typeof operator returns string denoting the type of variable being operand.
Following strings can be returned:
- “undefined” – if the value is undefined,
- “boolean” – if the value is false or true,
- “string” – if the value is a string,
- “number” – if the value is a number,
- “object” – if the value is an object (including arrays) or null,
- “function” – if the value is a function.
var a = "one", b = 2; console.log(typeof a); // string console.log(typeof b); // number
Data types meaning
The undefined value indicates that expected value is not defined and is only one value of the undefined type.
The null value is considered to be an empty object reference and is only one value of the null type.
Booleans, strings and numbers are other primitive values. Boolean represents logical truth and falsehood, string is sequence of Unicode characters, number simply represents all numbers.
Object is a collection of properties, where each property has a name and a value. Ordinary objects are unordered collections of named values.
An array is special kind of object, which contains an ordered collection of numbered values.
A function is another special kind of object which has executable code associated with it. Functions in JavaScript are true values and can be treated like regular objects.
Reply