Javascript Fundamental
JavaScript Fundamental
JavaScript is the language of the web. If you want to code for the web, we need to have knowledge of JS
Some language fundamental of JS includes:
- Variables
- Data Types
- Arithmetic, Assignment, and Comparision Operators.
- Urinary, Logical, Comma, and Spread Operator
- Operator Precedence
- Strict Mode
- Functions
JavaScript Data Types
Data types are basically what kind of data can be stored and manipulated. There are two types of data types in JavaScript.
- Primitive data type
- Non-primitive (reference) data type
Primitive Data Types
Primitive data types can hold only one value at a time. There are 5 types of primitive data types in JS.
Data Type | Description |
---|---|
String | represents a sequence of characters e.g.,“hello” |
Number | represents numeric values e.g. 100 |
Boolean | represents boolean value either false or true |
Undefined | represents an undefined value |
Null | represents null i.e, no value at all |
Non-primitive Data Types
It can hold collections of values and more complex entities. The non-primitive data types are as follows:
Data Type | Description |
---|---|
Object | represents an instance through which we can access members |
Array | represents a group of similar values |
RegExp | represents a regular expression |
Variables and Operators
JavaScript Variables
A variable is an identifier whose value may change during the execution of the program. There are two types of variables in JavaScript: local variable and global variable.
- Local Variable: It is declared inside the block or function. It is accessible within the function or block only.
- Global Variable: It is declared outside function or declared with window object. It is accessible from any function.
There are some rules while declaring a JavaScript variable.
- All variable names must start with a letter (a to z or A to Z), an underscore(_), or a dollar sign($).
- After the first letter, we can use digits (0 to 9). E.g., value1.
- JavaScript variables are case-sensitive. E.g., a and A are different variables.
- Reserved words cannot be used as a variable name. E.g., char, double, else, etc., are some reserved words.
Operators
An operator is a symbol that signifies the operations. In the expression 4+5 is equal to 9, here 4 and 5 are called operands, and ‘+’ is called operator.
JavaScript supports the following types of operators.
- Arithmetic Operator
- Comparison Operator
- Logical Operator
- Bitwise Operator
- Assignment Operator
1. Arithmetic Operator: It is a symbol used for basic mathematical calculations.
Operator | Purpose | Example |
---|---|---|
+ | Addition | a+b |
- | Subtraction | a-b |
* | Multiplication | a*b |
/ | Division | a/b |
% | Remainder | a%b |
++ | Increment | a++ |
– | Decrement | a– |
2. Comparison Operator: It is a symbol that is used to compare the values of two different operands.
Operator | Purpose | Example |
---|---|---|
== | Equals to | a==b |
!= | Not equal to | a!=b |
« | Less than | a<b |
<= | Less than or equal to | a<=b |
> | Greater than | a>b |
>= | Greater than or equal to | a>=b |
3. Logical Operator: It is a symbol that logically connects the logical expressions i.e. it is used to connect two or more expressions.
Operator | Purpose | Example | What it evaluates to |
---|---|---|---|
&& | Logical AND | a && b | True (1) only if both a and b are ture; else, false (0) |
|| | Logical OR | a || b | True (1) if either a or b is ture; false (0) only if both are false |
| | NOT | !a | False (0) if a is true; true (1) if a is false |
4. Bitwise Operator: It is used to perform the calculation using binary digits which can be performed using following operators.
Operator | Purpose |
---|---|
& | bitwise AND |
| | bitwise OR |
^ | bitwise XOR |
~ | bitwise complement |
« | left shift |
» | right shift |
»> | Right shift with zero |
5. Assignment Operator: It is a symbol used to assign a value or a result of an expression to an identifier.
Operator | Purpose | Example |
---|---|---|
= | Simple equals to | c=a+b |
+= | Add and assignment | a=+b is same as a=a+b |
-= | Subtract and assignment | a-=b is same as a=a-b |
*= | Multiply and assignment | a*=b is same as a=a*b |
/= | Divide and assignment | a/=b is same as a=a/b |
%= | Takes modulus and assignment (only for integer division) | a%=b is same as a=a%b |