JavaScript is a dynamic or loosely typed language, means we don't need to specify type of the variable because it is dynamically specified by JavaScript engine based on the type of input. We just need to use var or let keyword to create variable. There are basically two types of data type in JavaScript, which are given below-
Primitive Data Types:- There are besically five types of primitive data type in JavaScript, which are given below-
Non-primitive or Reference Data Types There are besically two types of non-primitive data type in JavaScript, which are given below-
| Data Types | Description |
|---|---|
| String | It represents any string (example "hello"). |
| Number | It represents any numeric value (example 777). |
| Boolean | It represents boolean value (example true or false). |
| Undefined | It represents undefined value. |
| Null | It represents null value. |
JavaScript keywords are reserved words, which we cannot use to name the variables or functions. There are total of 63 keywords which JavaSscript provides to the programmers.
Keywords, which are marked with * symbols are the new keywords in ECMAScript 5 and 6.
| Keywords | |||
|---|---|---|---|
| abstract | arguments | boolean | break |
| byte | case | catch | char |
| const | continue | class* | debugger |
| default | delete | do | double |
| else | eval | enum* | export* |
| extends* | false | final | finally |
| float | for | function | goto |
| if | implements | in | instanceof |
| int | interface | import* | let |
| long | native | new | null |
| package | private | protected | public |
| return | short | static | switch |
| synchronized | super* | this | throw |
| throws | transient | true | try |
| typeof | var | void | volatile |
| while | with | yield | - |
Use typeof to check primitive types and instanceof to check object types.
// typeof - returns a string
console.log(typeof 42); // "number"
console.log(typeof 'hello'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (historical bug in JS!)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
console.log(typeof Symbol()); // "symbol"
console.log(typeof 42n); // "bigint"
// instanceof - checks prototype chain
console.log([] instanceof Array); // true
console.log({} instanceof Object); // true
console.log([] instanceof Object); // true (arrays are objects)
// Best way to check for array
console.log(Array.isArray([])); // true
console.log(Array.isArray({})); // false
// Null check
const val = null;
console.log(val === null); // true (use strict equality)
// Symbol - unique, immutable identifier (ES6)
const id1 = Symbol('id');
const id2 = Symbol('id');
console.log(id1 === id2); // false - every Symbol is unique
// Useful as unique object keys
const USER_ID = Symbol('userId');
const user = { [USER_ID]: 123, name: 'Alice' };
console.log(user[USER_ID]); // 123
// BigInt - integers larger than Number.MAX_SAFE_INTEGER (ES2020)
const bigNum = 9007199254740991n; // note the 'n' suffix
const bigger = bigNum + 1n;
console.log(bigger); // 9007199254740992n
// Cannot mix BigInt with regular numbers
// console.log(bigNum + 1); // TypeError
console.log(Number(bigNum) + 1); // convert first
// Practical: cryptography, database IDs, financial calculations
const MAX_SAFE = Number.MAX_SAFE_INTEGER;
console.log(MAX_SAFE + 1 === MAX_SAFE + 2); // true (precision lost!)
const bigSafe = BigInt(MAX_SAFE);
console.log(bigSafe + 1n === bigSafe + 2n); // false (correct)
Explore 500+ free tutorials across 20+ languages and frameworks.