-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplicit-coercion.js
More file actions
22 lines (19 loc) · 884 Bytes
/
implicit-coercion.js
File metadata and controls
22 lines (19 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* CH. 01 - Essential Patterns
* Implicit Coercion
*
* JavaScript Patterns - modern JS patterns with ES5 & ES6 examples
* @copyright 2016 - 2017, Ahad Bokhari
* license: MIT
*/
/* (i.) JavaScript can be surprisingly forgiving when it comes to type errors, be wary of this in your applications */
5 + true; // => 6
/* (ii.) In many cases rather than raising an error JavaScript coerces a value to the expected type by following automatic processes. When you mix numbers and strings, JavaScript breaks in favor of strings */
5 + "5" // => 55
"5" + 5 // => 55
/* (iii.) Avoid using '==' with mixed types, this helps the reader to keep clear there is no conversion involved in the comparison */
const today = new Date();
if (form.month.value === (today.getMonth() + 1) && // strict '==='
form.day.value === today.getDate()) { // strict '==='
//..do something
}