###### tags: `JavaScript` `Notes`
# Intro to JS
## variables
use const until you can't, then use let; don't use var
### global
- redefine and redeclare
- we don't really use var anymorevar dog1 = 'Lilly1';
```
console.log(dog1);
dog1 = 'not Lilly1';
console.log(dog1);
var dog1 = 'some other thing';
console.log(dog1);
```
### const block-scoped {}
- can't redefine
- can't redeclare
```
const dog2 = 'Lilly2';
dog2 = 'something else';
const dog2 = 'something else';
```
### let block-scoped {}
- can redefine
- can't redeclare
```
let dog3 = 'Lilly3';
console.log(dog3);
dog3 = 'another dog';
console.log(dog3);
let dog3 = 'another dog';
```
### global object
`console.log(window);`
### math object
```
console.log(Math);
```
#### Basic Operations
```
1 + 1; // returns 2
2 * 2; // returns 4
2 - 2; // returns 0
2 / 2; // returns 1
const num1 = 2
const num2 = 2
num1 + num2 // returns 4
const string1 = 'My name is'
const string2 = 'Bob'
string1 + string2 // returns 'My name is Bob'
```
#### Modulo Operator
```
21 % 5; // returns 1
21 % 6; // returns 3
21 % 7; // returns 0
```
#### Math.pow
```
Math.pow(2,2) // returns 4
Math.pow(3,2) // returns 9
Math.pow(3,3) // returns 27
```
#### Math.floor Math.round Math.ceil
```
Math.round(6.5) // returns 7
Math.round(6.45) // returns 6
Math.floor(6.999) // returns 6
Math.ceil(6.0001) // returns 7
```
#### Truthiness
```
// items that are interpreted as true
true
1
' '
[] // an array, you'll learn more about this later
{} // an object, you'll learn more about this later
function() {}
// items that are interpreted as false
false
0
undefined
null
''
```
## logical operators
- && is 'AND'
- || is 'OR'
- ! bang operator, means 'NOT'
- === is deep equality
- == is non-type specific equality
- = is the assignment operator; how you define variables
- < > greater/less than
- >= <= greater or equal to/less than or equal to
## control flow - if/elseif/else statements or switch statements
pet grooming sorter; dogs under 10 lbs give a string 'pretty small dog haircut'
dogs bigger than 10 less than 50 give a string 'pretty medium dog haircut'
dogs bigger than 50 give a string 'pretty big dog haircut'
```
const dogWeight = 9; // '9'
const coatStyle = 'double';
if(dogWeight < 10) {
if(coatStyle === 'short') {
console.log('Pretty small dog haircut for a short coat');
}
else if(coatStyle === 'double') {
console.log('Pretty small dog haircut for a double coat');
}
else {
console.log('Pretty small dog haircut');
}
}
else if(dogWeight >= 10 && dogWeight <= 50) {
console.log('Pretty medium dog haircut');
}
else if(dogWeight > 50) {
console.log('Pretty big dog haircut');
}
else {
console.log('Go see the manager, you have a weird dog.');
}
```
### Examples
```
var coinFlip = Math.round(Math.random());
if (coinFlip === 1){
console.log("Heads")
} else{
console.log("Tails")
}
```
## for loop
- first part is declaring your iterator variable; usually let i = 0;
- second part is when you want the loop to stop
- third part is how to handle the iterator variable after completion of a run through
```
for (let i = 0 ; i < 10 ; i++ ) {
// | declare a var | conditional expression | increment var|
console.log(i);
}
for (let i = 0; i < 5; i++) {
// start at 0 | loop until 5 | add one every time
```
```
let str = 0;
for (let i = 0; i < 9; i++) {
str = str + i;
console.log(str);
}
```
## while loop
- continues to run until the value in paren is false
```
let n = 0;
while (n < 3) {
n++;
console.log(n);
}
```
## Examples
- console.log() the values in reverse order
- how to access an array index: arrayValName[0]
```
const array = [1,2,3,4,5,6,7,8,9,10];
for (let i = array.length - 1; i >= 0; i--) {
console.log(array[i]);
}
```
## functions
parameters are like local variables; they only exist inside the function
arguments are how you associate a value to the parameter
you can only return one thing
you need to return something, otherwise it'll return undefined
```
function add(num1, num2, num3) {
console.log(num1);
console.log(num2);
console.log(num3);
return num1 + num2 + num3;
}
console.log(add(2, null, 1));
```
## Example
- create a function that takes 2 params for dog weight and dog coat style
- return unique strings for each case
- dogCoatStyle can be short or long
```
function groomer(dogWeight, dogCoatStyle) {
if (dogWeight < 10 && dogCoatStyle === 'short') {
return ("The small dog gets a bath and nails clipped!");
} else if(dogWeight < 10 && dogCoatStyle === 'long') {
return("The small dog gets a bath and nails clipped plus haircut!");
} else if (dogWeight >= 10 && dogWeight <= 50 && dogCoatStyle === 'short') {
return ("The medium dog gets a bath and nails clipped!");
} else if (dogWeight >= 10 && dogWeight <= 50 && dogCoatStyle === 'long') {
return ("The medium dog gets a bath and nails clipped plus haircut!");
} else if (dogWeight > 50 && dogCoatStyle === 'short') {
return ("The big dog gets a bath and nails clipped!");
} else if (dogWeight > 50 && dogCoatStyle === 'long') {
return ("The big dog gets a bath and nails clipped plus haircut!");
} else {
return ("Go see the manager, you have a weird dog.");
}
}
console.log(groomer(59, 'long'));
console.log(groomer(8, 'short'));
console.log(groomer(24, 'short'));
```