var a = 1; // type int var b = '2'; // type of string console.log(a> b); // result returned as `false`. Thus, the variable `b` is interpreted as type` int` console.log(b + a); // result will be "21". The variable `a` is interpreted with the type` string` ('2' + '1' = '21')
In another example, when the program wants the value of a variable to be returned as a type
boolean, while that variable is defined with another type, the value of the variable will be interpreted in two truthy and falsy value groups .
var a = 1; // type int console.log(a == false) // when comparing `a` (type` int`) to `false` (` `boolean`), the program wants a to be interpreted as` boolean`, then the value truthy or falsy of variables will be considered // result returned as `false`. Why is it like that, I will explain below: D
So what is truthy and falsy value: -?
Perhaps this will be one of the most frequently asked questions in Javascipt interviews. And even myself and some of the others will be puzzled by this question because you don’t remember the exact values or you don’t know it. So today I am determined to consolidate my knowledge and share with everyone about this issue, hoping to help you somewhat. (bow)
To put it simply …
true. In contrast,
Falsy valueare the values that when cast to Boolean, the value is
falsy, all other values which are not considered are considered
Booleanhas a value
false, therefore, a false value is definitely a falsy value.
Numbercontains all the numbers. But there are two special values: zero (0) and Not a Number (NaN), and it is both falsy value.
String, with empty strings (the string does not contain any characters) is the falsy value.
Bottom of the skirt is like this
DatatypesFalsy valueBooleanfalseNumber0 or NaNString’’ or ""nullnullundefinedundefined
Accomplished! I have listed it
6 falsy values, all the other values are truthy.
For example some values are
Looking at these examples, you can explain the examples I wrote above
falsy valueusing operators,
==unintended cases can occur.
0empty numbers and strings
undefinedare equivalent but they are not equivalent to any other value.
NaNis not equivalent to any value - including principal
Infinityis truthy but when compared to
false, the result always returns
truewill result in
false, compared with that
// all true false == 0; 0 == ''; null == undefined;  == false; !! == true; // all false false == null; NaN == NaN; Infinity == true;  == true;  == true;
The only exception is
NaNstill not equivalent to any value, including itself.
When you need to compare a value with
boolean, you rarely need to compare them directly with
falsejust consider the value as truthy or falsy .
// instead of if (x == false) // ... // runs if x is false, 0, '', or  // use if (!x) // ... // runs if x is false, 0, '', NaN, null or undefined
When you need to compare two values are the same or not, you should use the comparison operator
!==) instead of
!=) to avoid having problems of converting the value type.
// instead of if (x == y) // ... // runs if x and y are both truthy or both falsy // e.g. x = null and y = undefined // use if (x === y) // ... // runs if x and y are identical... // except when both are NaN
Any value can be converted to value
booleanusing the operator
!! (Double Bangs). Use this operator for
'' or "",
NaN) will receive the value of
false, and are used when you want to check the value 2 is
// instead of if (x === y) // ... // runs if x and y are identical... // except when both are NaN // use if (!!x === !!y) // ... // runs if x and y are identical... // including when either or both are NaN
Does your business need an interactive website or app?
The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.
For more inquiry click here https://bit.ly/31eZyDZ
Book Free Interview: https://bit.ly/3dDShFg
With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.
The compound assignment operators consist of a binary operator and the simple assignment operator.
The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).
It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.
You can also check my video tutorial compound assignment operators.
Let’s consider an example. Suppose price = 5 and we want to add ten more to it.
var price = 5;
price = price + 10;
We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.
price += 5;
Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.
Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.
price -= 5;
Lets multiply price and show it.
and finally we will divide it.
If you have any doubts, comment below.