# JavaScript Notes

### **Variable Declaration in JavaScript**

In JavaScript, we have three keywords for variable declaration: var, let, and const.

1. `var` --&gt; This keyword is used to declare a variable and optionally initialize it with a value.
    
2. `let` --&gt; his keyword is used to declare a variable and optionally initialize it with a value.
    
3. `const` --&gt; his keyword is used to declare a variable and optionally initialize it with a value.
    

### Rules for Variable Names

1. Variable names cannot start with a numeric digit, but they can contain numerals in other positions.
    
2. Variable names are case-sensitive, so 'variable' and 'Variable' are considered distinct identifiers.
    

### Scope of variables

JavaScript variables can have three possible scopes:

* Global scope --&gt; The default scope for variables declared using 'var' outside any function or block. Global variables can be accessed throughout the entire code.
    
* Function scope --&gt; Variables declared using 'var' inside a function are limited to the function's scope, making them inaccessible outside the function.
    
* Block Scope --&gt; Variables declared using 'let' and 'const' inside a block (enclosed by curly braces) have block scope. They are accessible only within that block and its sub-blocks.
    

Global variables are properties of a global object which can be accessed using `this` or `globalThis` in all JavaScript environments. These global variables provide a lot of functionalities builtin into the runtime. The functions such as `setTimeout()`, `setInterval()`, etc. are a part of this global object.

### Special characters in Strings

You can use some special characters when dealing with strings. These characters represent specific values within the string. These are often prefixed with a backslash (\\) and are a common feature across many programming languages. The most important ones are listed below:

| Character | Meaning |
| --- | --- |
| \\0 | null byte, EOF |
| \\n | new line |
| \\b | backspace |
| \\t | tab |
| \\v | vertical tab |
| \\' | apostrophe |
| \\\\ | backslash |
| \\xxx | char codes in octal (Latin encoding) |
| \\xXX | char codes in hexadecimal (Latin encoding) |
| \\xXXXX | char codes in hexadecimal (Unicode encoding) |

### Operators

Operators are used to perform operations on numbers and to evaluate expressions. There are several types of operators:

1. Arithmetic Operators
    
    * `+` addition
        
    * `-` subtraction
        
    * `/` division
        
    * `*` multiplication
        
    * `%` modulo
        
    * `++` increment
        
    * `--` decrement
        
    * `+` unary plus
        
    * `-` unary minus
        
    * `**` exponentiation or power
        
2. Comparison Operators
    
    * `==` equal
        
    * `===` strict equal (also compare types)
        
    * `!=` not equal
        
    * `!==` strict not equal (also compare types)
        
    * `>` greater than
        
    * `<` less than
        
    * `<=` less than or equal
        
    * `>=` greater than or equal
        
3. Logical Operators
    
    * `&&` logical and
        
    * `||` logical or
        
    * `!` logical not
        
    * `false && anything` evaluates to false (short-circuiting)
        
    * `true || anything` evaluates to true (short-circuiting)
        
    * `??` nullish coalescing (null then assign)
        
4. Bitwise Operators (Implicit conversion of operands are 32-bit binary )
    
    * `&` bitwise and
        
    * `|` bitwise or
        
    * `^` bitwise xor
        
    * `~` bitwise not
        
    * `<<` bitwise left shift (multiply by 2)
        
    * `>>` bitwise right shift (divide by 2)
        
    * `>>>` zero-fill right shift
        
5. Assignment Operators
    
    * `=` assignment
        
    * `+=` addition assignment
        
    * `-=` subtraction assignment
        
    * `*=` multiplication assignment
        
    * `/=` division assignment
        
    * `%=` modulo assignment
        
    * `**=` exponentiation assignment
        
    * `<<=` left shift assignment
        
    * `>>=` right shift assignment
        
    * `>>>=` zero-fill right shift assignment
        
    * `&&=` logical and assignment
        
    * `||=` logical or assignment
        
    * `&=` bitwise and assignment
        
    * `|=` bitwise or assignment
        
    * `^=` bitwise xor assignment
        
6. Ternary Operator
    
    * `condition ? val1 : val2`
        
7. Unary Operators
    
    * `delete` remove object property
        
    * `typeof` data type of variable
        
    * `void` return no value
        
8. Relational Operators
    
    * `in` value in the object
        
    * `objectName instanceof objectType` if objectName is of type objectType
        
9. Other Operators
    
    * `new` create an instance of object
        
    * `this` reference to this object
        
    * `super` access parent properties
        
    * `()` grouping
        
    * `{a, b} = object` accessing object properties to variables in object
        
    * `[a,b,c] = array` accessing array elements to arbitrary variables
        
10. Rest and Spread
    
    * `...` remaining elements.
        
    * parameters --&gt; rest, arguments or destructuring --&gt; spread
        

### Conversion of Numbers to Different Number Systems

To convert numbers to different number systems, we can use '`parseInt()`' and '`toString()`' methods. '`parseInt()`' converts a number in any system to a decimal using the radix argument. '`toString()`' returns the string representation of the number in the specified radix number system. See the examples below:

```javascript
let a = 1101; //13 in binary
let b = parseInt(a,2); //returns 13
b.toString(2);//returns 1101
```
