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
- This data type represents a numeric value. It can store both integers and floating-point values.
- Its range is from -253 -1 to 253 -1.
2. BigInt
- It is used to represent integers that are larger than the Number data type.
- Its range is more than -253 -1 and more than 253 -1.
- To represent the given integer as BigInt, we have to suffix 'n' after the integer. Example: 10 is a number type, and 10n is a BigInt type.
3. Boolean
- This data type represents a logical entity and can only have two values: true or false.
4. Null
- This data type represents a null or empty value.
- It is used to mark the memory block empty intentionally.
5. Undefined
- This data type represents an uninitialized value.
- When a memory block is uninitialized, the JS engine implicitly initializes that memory block with 'undefined' in the variable phase.
- For variables declared with var, it will initialize it in the variable phase.
- For variables declared with let and const, it will not initialize it in the variable phase.
6. NaN
- It stands for 'not a number'.
- It represents a computational error.
- When the JS engine is not able to compute a result, it returns NaN.
- Example: "Hello" + 1 = "Hello1" and "Hello" - 1 = NaN.
7. Symbol
- It represents a unique identifier.
- We have the Symbol function, which is used to generate unique identifiers in our program.
8. String
- It represents a collection of characters.
- We have two types of strings: single-line and multi-line strings.
- Single-line string:
- It is enclosed by single quotes (' ') and double quotes (" ").
- It does not allow line breaks and whitespaces.
- Multi-line string:
- It is enclosed by backticks (` `).
- It is also called a template string.
- Template strings allow us to insert variables and expressions directly in the string using `${variable_name}` notation.
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.