Data Types and Keywords in JavaScript — Complete Guide | Tutorials Logic
JavaScript Data Types
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-
| 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. |
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 |
|---|---|
| Array | It represents multiple values of same or different data types in a single variable. |
| Object | It represents instance by which we can access members. |
JavaScript Keywords
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 | |||
|---|---|---|---|
| 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 | - |
Keywords, which are marked with * symbols are the new keywords in ECMAScript 5 and 6.
Type Checking with typeof and instanceof
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)
ES6+ New Data Types - Symbol and BigInt
// 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)
- JavaScript has 8 data types: string, number, boolean, undefined, null, object, symbol, and bigint.
- typeof null returns "object" - this is a known historical bug in JavaScript.
- Use Array.isArray() to check for arrays, not typeof (which returns "object" for arrays).
- Symbol creates a guaranteed unique value - useful for private object keys and avoiding naming collisions.
- BigInt handles integers beyond Number.MAX_SAFE_INTEGER (2^53 - 1) without precision loss.
- Reserved keywords cannot be used as variable names - they have special meaning in the language.
Level Up Your Javascript Skills
Master Javascript with these hand-picked resources