Javascript Tokens
By Saket Bhatnagar••Beginner to Intermediate
token
- 1It is the smallest unit of programming language.
- 2We have 5 types of operators,punctuators,keywords ,identifiers , literals.
Operators
- 1These are symbols or words that represent mathematical, logical, or other operations.
- 2Examples include : +, -, *, /, =, ==, ===, &&, ||, !, and typeof.
Punctuators
- 1These are symbols used to group, separate, or punctuate code.
- 2Examples include parentheses (), curly braces {}, square brackets [], commas ,, semicolons ;, and the period . (used to access object properties).
keywords
- 1These are reserved words that have a special meaning in the language.
- 2Examples like if, else, for, while, function, and return,etc.
identifiers
- 1These are user given names to variables, functions, and other objects in the code.
- 2Identifier name can not start with number.
- 3Identifier name should not be a keyword
- 4If Identifier is of multiple word, instead of using space, we have to use underscore.
- 5identifier name should not have special character but can start with underscore(_) and dollar($).
literals
- 1These are values used in our program like number(2),string('hello world') , etc.
Types of literals / Datatypes
- 1Primitive
- 2Non-Primitive
Primitive literals
- 1In JavaScript, a primitive data type is a data type that represents a single value.
- 2JavaScript treats primitive values as immutable values, means that 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 new value and assigning it to a variable. Here , variable will hold the reference of latest object with new value and the previous object with it's value will garbage collected.
- 3We have 8 primitive types of literals -number , bigint , boolean , nan , undefined , null , symbol , string..
Primitive datatypes
Number
- 1This data type represents a numeric value. It can store both integers and floating-point values.
- 2It's range is from -253-1 to 2 53-1 .
BigInt
- 1It is used to represent integers that are larger than the Number data type
- 2It's range is more than -253-1 and more than 253-1 .
- 3To represent the given integer as bigint , we have to suffix 'n' after the integer.
Example : 10 is number type and 10n is bigint type.
Boolean
- 1This datatype represents a logical entity and can only have two values: true or false.
Null
- 1This datatype represents a null or empty value.
- 2It is used to mark the memory block empty intentionally.
Undefined
- 1This datatype represents an uninitialized value.
- 2When memory block is unintialized , js engine implicitly initialize that memory block with 'undefined' in variable phase.
- 3For variable declared with 'var' it will initialize it in variable phase
- 4For variable declared with 'let' and 'const' it will not initialize it in variable phase.
NaN
- 1It stands for 'not a number'.
- 2It represents computational error.
- 3When js engine is not able compute result it returns 'NaN'.
- 4Example : "Hello" + 1 = Hello1 and "Hello" - 1 = NaN
In first case , js engine concatnated the string with number.
In second case , js engine is able to compute anything because we can not subtract 1 from "Hello" string therefore it returns NaN.
Symbol
- 1It represents a unique identifier.
- 2We have Symbol function which is used to generate unique idenitifiers in our program.
String
- 1It represents collection of characters.
- 2We have two types of strings :- single line and multi line string.
- 3Single line string :
- It is enclosed by single quotes (' ') and double quotes (" ") .
- It doesnot allow line breaks and whitespaces. - 4Multi line string :
- It is enclosed by backticks (` `).
- It allow line breaks and whitespaces.
- It is also called as template string.
- Template strings allow us to insert variables and expressions directly in the string using ` ${ variable_name } ` notation.
Non-Primitive literals
- 1In JavaScript, a non primitive data type is a data type that represents multi value.
- 2JavaScript treats non-primitive values as mutable values, means that their value can be changed. When we try to update a value , new object is not created . Here value is changed in the same memory block.
- 3Non-primitive datatype : object ,array , etc