Basic JavaScript (Part-1)
Published: 2021-05-05 14:38:32

## Data Types
There are two basic data types in Js. They mainly represent two different ways that data are stored in memory.

**Primitive Data Types:** data stored as a single value and they are immutable.
* string ('Hi!', "Hello")
* number (12,14.23)
* BigInt (22n)
* boolean (true, false)
* symbol
* null
* undefined.

**Non-Primitive Data Type:** data stored as value correspond to memory reference and the values are mutable.
* Object

## Checking Data Type
To check the data type of a variable or literal, we can use the typeof operator. 
Syntax: typeof operand
It will return the type.

```javascript
console.log(typeof 23); //"number"
console.log(typeof 23); //"number"
console.log(typeof NaN); //"number"

console.log(typeof "Hi!"); //"string"
console.log(typeof true); //"boolean"
console.log(typeof false); //"boolean"

console.log(typeof {a: 1}); //"object"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object"

```
## Dynamically Typed Language
In a programming language, if variables value are not bound to single data types, then it is called dynamically typed language. Javascript is a dynamically typed language. A variable can number and then it can be reassigned to string or other data types.
```javascript
// no error
let greetings = "hi";
greetings = 789;
```

## Number
There is a total of five types of numerical values in Number Type.
1. **integer**
2. **float**
```javascript
let value = 456; // integer
value = 4.56; // floating point number
```
Others three are special Numeric values.
3. **Infinity:** It is greater than any number.
4. **-Infinity:** It is lower than any number.
5. **NaN:** It means there is a computational error or incorrect or undefined operational result.
```javascript
let value = ( 1 / 0 ); // Infinity
value = "asdf" / 2 ; // NaN
```

## Number Parsing

We can parse a string to number using the parseInt and parseFloat function. they are top-level functions and not associated with any local object. They are the function property of the global object. The parseInt() function parses a string and returns an integer. The parseFloat() function parses a string and returns a floating-point number.
If the first character cannot be converted to a number, parseInt() and parseFloat() returns NaN.
```javascript
//Example of parseInt()
parseInt("11")
// Output: 11
parseInt("11.29")
// Output: 11
parseInt("10 20 30")
// Output: 10
parseInt("Hello29")
// Output: NaN


//Example of parseFloat()
parseFloat("11")
// Output: 11
parseFloat("11.29")
// Output: 11.29
parseFloat("10 20 30")
// Output: 10
parseFloat("Hello29")
// Output: NaN
```


## Number Rounding
There are built-in functions for rounding:
* **Math.floor():** Moves down.
* **Math.ceil():** Moves Up.
* **Math.round():** Rounds to the nearest integer
```javascript
Math.floor(2.1) // 2
Math.floor(-2.1) // -3

Math.ceil(2.1) // 3
Math.ceil(-2.1) //2

Math.round(4.1) // 4
Math.round(4.5) // 5
Math.round(3.6) // 4
```

## String
A string is a single or a series of characters. In javascript, strings are surrounded by quotes. There are three types of quotes.
1. **single Quotes** 
2. **Double Quotes**
3. **Backticks**
```javascript
let string1 = 'Single quotes are ok too';
let string2 = "Double Quotes";
let string3 = `Backticks Quotes`;
```
There are no difference between double and single quotes. They are simple quotes. But, Backticks strings are called template strings because variables and expressions can be embedded into them.
```javascript
let name = "Rishu";
let greetings = `Hi!, ${name}`;
let a = 10, b=15;
let sum = `Sum = ${a+b}`;
```


## Case Change
There are two very helpful functions to change the case of a text. 
* toLowerCase()
* toUpperCase();

Example of toLowerCase()
```javascript
let text = "HI!, EARTH";
console.log(text.toLowerCase());
// Output: "hi!, earth"
```

Example of toUpperCase()
```javascript
let text = "Hello, everyone";
console.log(text.toUpperCase());
// Output: "HELLO, EVERYONE"
```

## Trimming a string
In javascript, we can use the trim() method to remove whitespace (spaces, tabs, etc) from both the start and end of a string. It is a method of the String object. It returns a string without start and end whitespaces.
```javascript
let checkingString = "   Hello, How are you?     ";
console.log(checkingString.trim());
// Output: "Hello, How are you?"
```


## Check string
startsWith() and endsWith() are string method. With startsWith(), we can check Whether a string starts with the specified prefix and With endsWith(), we can check Whether a string ends with the specified suffix.

Example of startsWith()
```javascript
let checkingString = 'Hello World!';

console.log(checkingString.startsWith('Hello')); // true
console.log(checkingString.startsWith('Heppo')); // flase
```
Example of endsWith()
```javascript
let checkingString = 'Hello World!';

console.log(checkingString.endsWith('World!')); // true
console.log(checkingString.endsWith('World')); // flase
```