Tokens and Data Types in JavaScript

Tokens

1. It is the smallest unit of programming language.

2. We have 5 types of tokens: operators, punctuators, keywords, identifiers, literals.

Operators

1. These are symbols or words that represent mathematical, logical, or other operations.

2. Examples include: +, -, *, /, =, ==, ===, &&, ||, !, typeof.

Punctuators

1. These are symbols used to group, separate, or punctuate code.

2. Examples include: parentheses (), curly braces {}, square brackets [], commas ,, semicolons ;, and the period . (used to access object properties).

Keywords

1. These are reserved words that have a special meaning in the language.

2. Examples include: if, else, for, while, function, return, etc.

Identifiers

1. These are user-given names to variables, functions, and other objects in the code.

2. Identifier name cannot start with a number.

3. Identifier name should not be a keyword.

4. If the identifier is of multiple words, instead of using space, we have to use an underscore.

5. Identifier name should not have special characters but can start with an underscore (_) and dollar ($).

Literals

1. These are values used in our program like numbers (2), strings ('hello world'), etc.

Types of Literals / Data Types

1. Primitive

2. Non-Primitive

Primitive Literals

1. In JavaScript, a primitive data type is a data type that represents a single value.

2. JavaScript treats primitive values as immutable values, meaning their value cannot be changed. Instead, when you perform an operation that appears to modify a primitive value, you are actually creating a new object with a new value and assigning it to a variable. Here, the variable will hold the reference of the latest object with the new value, and the previous object with its value will be garbage collected.

3. We have 8 primitive types of literals: number, bigint, boolean, NaN, undefined, null, symbol, string.

Primitive Data Types

1. Number

2. BigInt

3. Boolean

4. Null

5. Undefined

6. NaN

7. Symbol

8. String

Non-Primitive Literals

1. In JavaScript, a non-primitive data type is a data type that represents multiple values.

2. JavaScript treats non-primitive values as mutable values, meaning their value can be changed. When we try to update a value, a new object is not created. Here, the value is changed in the same memory block.

3. Non-primitive data types: object, array, etc.