(Javascript) My learning journey Part 3: if / else and mores
Tags: javascript, beginners, tutorial
Published: 2021-02-22 13:29:24

---
title: (Javascript) My learning journey Part 3: if / else and mores
published: true
description: 
tags: javascript, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik1c8mr2sz8zi3g466dq.jpg
series: (Javascript) My learning journey
---

An essential point to remember a training course is to take notes and discuss the subject with others. That's why every day I will post on dev.to the concepts and notes that I learned the day before.

If you want to miss nothing click follow and you are welcome to comments and discuss with me.

Without further ado here is a summary of my notes for day 3.


<h2>Decisions Statements</h2>
```js
// Comparaison operators
== // equal no type check
=== // equal with type check
!= //not equal
!== // not equal with type check
|| //or
&& //and
> //greater than
>= // greater or equal to
< //less than
<= // less or equal to

const maxUsers = 6
let currentUsers = 4

// Execute code only if a condition is met
if (currentUsers < maxUsers) {
  currentUsers += 1
  console.log('New user logged in')
}

// if condition is not met do something else
if (currentUsers < maxUsers) {
  currentUsers += 1
  console.log('New user logged in')
} else if (currentUser === 1) {
  console.log('First one to logged in')
} else {
  console.log('User logged in')
}

// Shorthand one line if
if (currentUsers === 1) console.log('First user!')

// Falsy value
false, 0, '', undefined, null, NaN

// Truthy value
Everything not falsy include {}, [], "0"

// Exemple
console.log(Boolean('')) // false
console.log(Boolean(0)) // false
console.log(Boolean('Mike')) // true

const amount = 100
// amount will evaluate to true because is not falsy
if (amount) {
  console.log('The amount is: ' + amount)
}

let amount
// amount will evaluate to false because it's undefined
if (amount) {
  console.log('Big amount')
} else {
  console.log('No amount') // No amount
}

const age = 25

// Equality operator with no type check ==
console.log(age == '25') // true

// Equality operator with type check ===
console.log(age === '25') // false
console.log(age === 25) // true


// Switch statement
switch (job) {
  case 'Teacher':  // job === 'Teacher'
    console.log('Job : Teacher')
    break;
  case 'Programmer':
  case 'Designer':
    console.log('Job : Programmer or designer')
    break;
  default: // if no case match
    console.log('No job')
}

```
<h2>Expression vs Statement</h2>
A expression is a piece of code that return a value
Exemples:
```js
100 + 200 // return 300
0 // return false
1 // return true
true && !false // return true
```

Statement is syntax that perform action but do not return value
Exemple:
```js
if (200 > 100) { // if is a statement but 200 > 100 is a expression
   const number = 'High Number' // statement
}
```
<h2>Conditional operator or Ternary operator</h2>
Conditional operator are like if / else statement but in one line
```js
const isActive = true
// condition ? true : false
isActive ? console.log('Active') : console.log('Not active')

// assignment
status = isActive ? 'Active' : 'Not Active' // Active

// Ternary return a expression so can be use in string template
console.log(`This user is ${isActive ? 'Active' : 'Not Active'}`)

```

<h2>Prompt input</h2>
```js
// prompt return a string
const input = prompt("What's your age? ")
console.log(typeof input) // string
console.log(Number(input)) // 25

```

<h2>Strict Mode</h2>
Strict mode makes several changes to normal JavaScript:

Eliminates some JavaScript silent errors by changing them to throw errors.
Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
Prohibits some syntax likely to be defined in future versions of ECMAScript.

To use Strict Mode just insert 'use strict' statement at the very top of the JS script
```js
'use strict'

console.log(firstName) // throw error not define
```

<h2>Conclusion</h2>
That's it for part 3. Next will dive into functions. Stay Tune!