Equality
- Loose (non-)equality
- Checks for value equality with implicit coercion (coercion algorithm depends on types)
- One or both values are coerced (until the two types match) using a complex decision tree
- Strict (non-)equality
- Checks for both value and type equality (no coercion)
Type Conversion
Explicit coercion: controlled way of type conversion
Using complex primitive type constructors: String ( ) ,
Number ( ) , Date ( )
Implicit coercion: made by Javascript automatically
Writing to console: console . log (12 + HUF”) ;
Calling a method in a primitive type (like indexOf()), it’s
converted to its complex primitive type automatically
Performing loose equality check: 12 == “12”
Using unary operators: +sVar, ! ! svar
Overloading the OR operator
var sLocaIName = sName || “”:
if sName is null or undefined (coerced to false), an empty string will
be assigned to the variable. Otherwise, the value of
sName will be assigned
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title>JS Basics - Equality</title>
<script>
var a = 1;
var b = "1";
console.log( "a == b: " + (a == b) );
console.log( "a === b: " + (a === b) );
function checkTruth(variable) {
if(variable) {
console.log( "'" + variable + "' is true" );
} else {
console.log( "'" + variable + "' is false" );
}
}
checkTruth(a);
checkTruth("Text");
checkTruth(0);
checkTruth(-0);
checkTruth(NaN);
checkTruth("");
checkTruth(null);
checkTruth(undefined);
checkTruth(false);
//Coercion to string
var iString = 1;
new String(1);
1+"";
//Coercion to number
var sNum = "1";
new Number(sNum);
+sNum;
//Coercion to boolean
var sBoolean = "Hello";
new Boolean(sBoolean);
!!sBoolean;
//Overloading the OR operator
function setName(sName) {
this.sLocalName = sName || "";
console.log(this.sLocalName);
}
setName();
setName("World");
</script>
</head>
<body class="sapUiBody" id="content">
</body>
</html>