# JavaScript Fundamentals

Hey Guys,

Today we will start with the basic building blocks of every programming language, specifically in  JavaScript.
 
## Logical Operators

The logical operators are used to get boolean  results by comparing the operands.

```
||     -     OR
&&   -    AND
!       -    NOT
??     -    Nullish coalescing
```

Precedence of AND < OR

**|| - OR**

- Evaluates operand from left to right.
- For each operand converts it to boolean. If its true stops and returns the original value
- If all the operand returned is false then returns the last operand.

**&& - AND**

AND is the reciprocate of  OR. It returns the first false operand.

**! - NOT **

NOT is the opposite of its operand.

```
console.log(!true); // returns false
```
**?? - Nullish Coalescing**

It returns the first defined value which is not null or undefined.

*Different between ?? and ||*
 
- || returns the first truthy value
- ?? returns the first defined value
- Both || and ?? has same precedence
- While using ?? with other logical operators braces is must

## Loops

- When we want to execute same code n number of times there comes the loops. It will execute the statement n   no of times with the help of conditions.
- Some of the loops present in JavaScript

```
while(condition){
//loop body
}
---------------------------------------------------
for (initialize ; condition ; stepup ){
//loop body
}

----------------------------------------------------
do {
//loop body
} while (condition)
-----------------------------------------------------
switch(x)

case value 1:
//statement
break;
case value 2:
//statement

break;
.
.
.
default:
//statement
break;
```

- Above are the major loops present in loops.
- We can also nest loops to get our desired results.
- `Break` is used to break out of loop.
- `continue` is used to skip the current iteration(If a loop runs 10 times it has 10 iterations ) of loop.
-  We can use label to access any loop in the nested loops.
- In `switch` statement we can group cases and comparison is strict equals.

## Functions
- In programming we have a principle `Never repeat`
- Functions are used for that. If have a few lines of code which you would use few places in your program then functions comes in to picture.
```
function func_Name(){
//code
}
```
- We have functions to do some task or give some value. To give some value we use `return` statements.
- We have parameters to functions and arguments which are passed to those parameters.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1662909572989/FSiDvi5c6.png align="left")

```
function sum(a,b){
return a+b;
}

sum(a,b); // here a, b are arguments
```

## Function Expressions

Functions expression just an extension to functions does the same thing but the syntax is different.

```
Normal function 

function sum(a,b)
{
return a+b;
}

Function expressions

let sum=function(a,b)
{ 
return a+b
};

```
JavaScript function is a value here.

**Callback**

We can pass a function as a argument to another function it is called callback.
We can also not name the function and use it as `Anonymous` functions.

```
function SumAndAvg(a,b,c){
let sum= a+b;
let Avg= avg(sum);
return Avg;
}
function avg(sum){
return sum/2;
}
SumAndAvg(1,2,avg); // Here we are avg is used as callback 
```

**Arrow Functions**

It is same as function expressions but concise.

```
let sum=(a,b) => a+b;
```
## Resources

- https://javascript.info/

- https://developer.mozilla.org/en-US/docs/Web/JavaScript

- https://eloquentjavascript.net/

