Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Type coercion

console.log(true + false);        // 1
console.log(true + true + true);  // 3
++++++++++++++++++++++++++++++++++++++++
console.log(1 + "1");          // "11"
console.log(1 + 1);            // 2
console.log(1 + Number("1"));  // 2
++++++++++++++++++++++++++++++++++++++++
One extremely important consideration with respect to JavaScript's 
type coercion is the issue of equality. 
You might recall that there are two ways to 
check if two things are equal in JavaScript: == and ===. 
The important thing to realize is that using == implicitly coerces 
data types! This means that 1 == "1" will return true. 
In reality, these two things are not equal: one is a 
string and the other is a number. 
To get the proper result, you must use ===. 
This is why it's considered a best practice to always use === when checking
equality unless you have a specific reason not to. 
Doing so will ensure that you will never end up with two things
that are not really equal being treated as equal because 
JavaScript coerced them to the same data type.
Comment

PREVIOUS NEXT
Code Example
Javascript :: what difference between react func and class components 
Javascript :: javascript render jsx element x many times 
Javascript :: word randomizer 
Javascript :: what is redis 
Javascript :: lodash find all in array 
Javascript :: password regex javascript long way 
Javascript :: email validation node js 
Javascript :: javascript validate if string null undefined empty 
Javascript :: buildpack for nodejs 
Javascript :: js group by 
Javascript :: jquery onclick click 
Javascript :: apache react deploy "conf" 
Javascript :: get index of item with attribute javascript 
Javascript :: how do you swap the vaRIables js 
Javascript :: using cors as middleware in js 
Javascript :: Ternary Operator react 3 Conditions 
Javascript :: react native webview disable touch 
Javascript :: upload image to server next js 
Javascript :: how to stop canvas resizing from resizing images 
Javascript :: formik validate field array select 
Javascript :: sequelize get all data 
Javascript :: DevDependencies and dependencies syntax in Node package.json 
Javascript :: longitud objeto javascript 
Javascript :: js change text on hover 
Javascript :: javascript json trypass 
Javascript :: createelement with id 
Javascript :: submit form with ajax 
Javascript :: sort array without changing the original js 
Javascript :: jquery slider value 
Javascript :: dom event 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =