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.
Decisions Statements
// 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')
}
Expression vs Statement
A expression is a piece of code that return a value
Exemples:
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:
if (200 > 100) { // if is a statement but 200 > 100 is a expression
const number = 'High Number' // statement
}
Conditional operator or Ternary operator
Conditional operator are like if / else statement but in one line
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'}`)
Prompt input
// prompt return a string
const input = prompt("What's your age? ")
console.log(typeof input) // string
console.log(Number(input)) // 25
Strict Mode
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
'use strict'
console.log(firstName) // throw error not define
Conclusion
That's it for part 3. Next will dive into functions. Stay Tune!
Top comments (0)