Variable Declaration in JavaScript
In JavaScript, we have three keywords for variable declaration: var, let, and const.
var
--> This keyword is used to declare a variable and optionally initialize it with a value.let
--> his keyword is used to declare a variable and optionally initialize it with a value.const
--> his keyword is used to declare a variable and optionally initialize it with a value.
Rules for Variable Names
Variable names cannot start with a numeric digit, but they can contain numerals in other positions.
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 --> The default scope for variables declared using 'var' outside any function or block. Global variables can be accessed throughout the entire code.
Function scope --> Variables declared using 'var' inside a function are limited to the function's scope, making them inaccessible outside the function.
Block Scope --> 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:
Arithmetic Operators
+
addition-
subtraction/
division*
multiplication%
modulo++
increment--
decrement+
unary plus-
unary minus**
exponentiation or power
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
Logical Operators
&&
logical and||
logical or!
logical notfalse && anything
evaluates to false (short-circuiting)true || anything
evaluates to true (short-circuiting)??
nullish coalescing (null then assign)
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
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
Ternary Operator
condition ? val1 : val2
Unary Operators
delete
remove object propertytypeof
data type of variablevoid
return no value
Relational Operators
in
value in the objectobjectName instanceof objectType
if objectName is of type objectType
Other Operators
new
create an instance of objectthis
reference to this objectsuper
access parent properties()
grouping{a, b} = object
accessing object properties to variables in object[a,b,c] = array
accessing array elements to arbitrary variables
Rest and Spread
...
remaining elements.parameters --> rest, arguments or destructuring --> 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:
let a = 1101; //13 in binary
let b = parseInt(a,2); //returns 13
b.toString(2);//returns 1101