> For the complete documentation index, see [llms.txt](https://dtian.gitbook.io/daily-report-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dtian.gitbook.io/daily-report-1/day-34-9-10.md).

# Day 34 - 9/10

Plan: Continue learning JavaScript

Progress:&#x20;

### <mark style="color:blue;background-color:green;">Data types:</mark>&#x20;

* Js types are dynamic, which means the same var can have different data types
* when add number and string, the number will be treated as string; caculation still be done if there is no string before that
* array: `const name = [a, b, c];`
* objects: `const name = {a, b, c};` : works like struct
* undefined: a var without value hold value `undefined` and type `undefined`; string without value hold value `""` and type `undefined`

### <mark style="color:blue;background-color:green;">Function</mark>&#x20;

* `function`` `*`name`*`(`*`parameter1, parameter2, parameter3`*`) {`\
  &#x20; `//`` `*`code to be executed`*\
  `}`
* Accessing a function without () returns the whole function, not the result: `let value = toCelsius;`
* Functions can be used the same way as variables, in all types of formulas, assignments, and calculations.

### <mark style="color:blue;background-color:green;">Objects</mark>

* const name = {property:property name, ... }
* A method is a function stored as a property.
* `this` keyword
* do not declare Strings, numbers, and booleans as object

### <mark style="color:blue;background-color:green;">Events</mark>&#x20;

* Js event is how Js react when sth happen to Html elements (click a button, web page finished loading, ...)
* &#x20;event are commonly used in function rather than directly used

### <mark style="color:blue;background-color:green;">String</mark>

* let string = string.length;
* turn special characters to string: `\' ~ '` ; `\" ~ "`

### <mark style="color:blue;background-color:green;">String method</mark>

#### <mark style="color:blue;">Extract string parts:</mark>&#x20;

* slice(start, end);  :&#x20;

  * if 'end' is omitted, the method will slice out from 'start' to the rest
  * if 'end' is omitted, 'start' is negative, the position will be counted from the end
  * 'end' and 'start' is negative works like positive

* substring(start, end); : works as slice but negative values are treated as 0

* substr(start, length); similar to slice

#### <mark style="color:blue;">Replace string</mark>&#x20;

`let text = "Please visit Microsoft!";`\
`let newText = text.replace("Microsoft", "W3Schools");`

* replaces a specified value with another value in a string; returns a new string, does not change the string it is called on; and <mark style="color:orange;">only replace the first match</mark>; <mark style="color:orange;">case sensitive</mark>
* use `/specified value/i` flag to remove case sensitive
* use `/specified value/g` flag to replace all matches
* text = text.replaceAll("Cats","Dogs");

Uppercase / Lowercase

* toUpperCase() : the whole string to uppercase
* toLowerCase() ; the whole string to lower case

#### <mark style="color:blue;">Concat</mark>

* join 2 or more string

`let text3 = text1.concat(" ", text2);`

#### <mark style="color:blue;">Trim</mark>

* remove whitespace from head and tail
* trimStart() / trimEnd() : remove whitespace from head or tail only

#### <mark style="color:blue;">String padding</mark>

* padStart() / padEnd(): pad head or tail with character "y" until reach length x&#x20;

`let padded = text.padStart(x,"y");`

* to padding number, it needed to convert to string first

`let numb = 5;`&#x20;

`let text = numb.toString();`&#x20;

`let padded = text.padStart(4,"0");`

#### <mark style="color:blue;">Extracting characters</mark>

* `charAt(`*`position`*`) :` return the character
* `charCodeAt(`*`position`*`) :` return the UTF-16 code of the character

### <mark style="color:blue;background-color:green;">String search</mark>

* `indexOf()` return the first position of a string in a string; search forward
* `lastIndexOf()` return the last position of a string in a string; search backward
* Both `indexOf()`, and `lastIndexOf()` return -1 if the text is not found
* a second parameter is added to declare the position to start searching:

`text.lastIndexOf("locate", 15);`

* `search()`&#x20;

#### <mark style="color:blue;">String match</mark>

* `.match("string");`  : returns an array containing the results of matching a string against a string
* `.match(/ain/gi);` : use /g , /i flag to find all matches despite uppercase or lowercase
* `.matchAll()` : return every matches in order
* `.includes("specified value", position)` : return true / false whether the value is included or not, search from position forward (optional); case sensitive
* `.startsWith()` : return True / false; check if the string starts with specified value or not; a second parameter to set the start position (optional); case sensitive
* `.endsWith()`

### <mark style="color:blue;background-color:green;">Numbers</mark>

#### <mark style="color:blue;">Integer Precision</mark>

* Integers (numbers without a period or exponent notation) are accurate up to 15 digits

#### <mark style="color:blue;">Floating Precision</mark>

* Floating point arithmetic is not always 100% accurate; to solve this, multiply the parameter, calculate then divide

#### <mark style="color:blue;">Not a Number</mark>

* do arithmetic with a non-numeric string result in NaN
* `isNaN()` check a value is or isn't a number&#x20;
* `typeof NaN` is number

#### <mark style="color:blue;">Infinity (or -Infinity)</mark>

* the value return when calculate a number outside the largest possible number
* Infinity is a number

#### <mark style="color:blue;">Hexadecimal</mark>&#x20;

* hexadecimal constant preceded by `0x`
* `.toString() :` to convert number among bases (base 2, base 10, base 8, ...)

#### <mark style="color:blue;">Numbers as Objects</mark>

* numbers can be defined as objects but DON'T
* objects can't be compared with objects
* compare 2 objects alway return false

### <mark style="color:blue;background-color:green;">Number Method</mark>

* `.toString()` : convert number to string
* `.toExponential(x)` : rounded number to x numbers behind decimal point
* `.toFixed(x)` : number of decimal&#x20;
* `.toPrecision(x)` : number with length x
* `.valueOf()` : return a number as a number
* `Number()` : convert JavaScript variables to numbers
* `parseInt()` : parse a string and return the first number, float not included
* parseFloat() : parse a string and return the first number
