What does a !! in JavaScript mean? [duplicate] - javascript

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
myVar = !!someOtherVar
What does the !! operator (double exclamation point) mean in JavaScript?
Came across this line of code
strict = !!argStrict
... here and wondered what effect the !! has on the line? Pretty new to JS!

It converts your value to a boolean type:
var x = '1';
var y = !!x;
// (typeof y === 'boolean')
Also note the following:
var x = 0;
var y = '0'; // non empty string is truthy
var z = '';
console.log(!!x); // false
console.log(!!y); // true
console.log(!!z); // false

It converts the value to a value of the boolean type by negating it twice. It's used when you want to make sure that a value is a boolean value, and not a value of another type.
In JS everything that deals with booleans accepts values of other types, and some can even return non-booleans (for instance, || and &&). ! however always returns a boolean value so it can be used to convert things to boolean.

It is a pair of logical not operators.
It converts a falsey value (such as 0 or false) to true and then false and a truthy value (such as true or "hello") to false and then true.
The net result is you get a boolean version of whatever the value is.

It converts to boolean

Its a "not not" arg
commonly used to convert (shortcut) string values to bool
like this..
if(!!'true') { alert('its true')}

Related

When should we use '=== ' operator in javascript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 6 years ago.
When and why should we use '===' in javascript or jquery.
Is it recommended to test string using === and if yes why.
I have a code where i am checking a condition on the string like.
if(a == "some-thing") is this right or should i use '==='
=== means exactly equal - compare value and type.
'1' == 1 // true
'1' === 1 // false
=== is used for strictly type check, when you want to check value as well as its type.
For Example
var x = 5;
var y = "5";
var z=5;
alert(x==y);//string and int value same though type different - TRUE
alert(x===y);//value same but type different - FALSE
alert(x===z);//value same and type same- TRUE
More Information
with === you will compare the value AND the type of data. With == you only compare the value. So 1000 == "1000" it's true, while 1000 === "1000" is false.
In a simple answer:
Triple equals does a check for type equality. So it'll check the types as well as the contents.
e.g.
var a = '1' === 1;
Would be false.
A double equals would only check the contents, so the code above would return true.
As a good practice, as much as possible.
As a minimum : if you want to check specifically for 0/false/null value because when you use == they're all the same :
0 == false // true
0 === false // false
And f you test the existence of a value but that value can be false or 0 this won't work.
if(value){} // false is value === false or value === 0
There is also the type equality but i don't really think that much relevant unless you depend on some 3rd-party where you need this.
A === "some string" means equal value and moreover equal type.
It's a double test: In your case you obtain true not simply if the values are equal but moreover if the variable A is a string.
You should almost always use the === operator.
An example why:
1 == '1' //is true
1 === '1' //is false
And you want to achieve the second result, because === checks if also the type is the same.

Javascript difference between "=" and "===" [duplicate]

This question already has answers here:
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 7 years ago.
I struggle to understand the function below. I didn't know why my script wasn't working until I changed = with === in the if statement, as shown below. Why does === work while = doesn't?
var testTest = function(answer) {
if (answer === "doggies") {
return "My favorite animal!";
} else {
return "Tested";
}
};
testTest("doggies")
When I type doggies, it shows me My favorite animal! With anything else, it returns Tested as it should.
However, when I change the === in the if statement with =, the else part doesn't work.
var testTest = function(answer) {
if (answer = "doggies") {
return "My favorite animal!";
} else {
return "Tested";
}
};
testTest("elephant")
You need to use == or === for equality checking. = is the assignment operator.
You can read about assignment operators here on MDN.
As a quick reference as you are learning JS:
= assignment operator
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
I assume you know that = is for assignment, after all, you are using assignment already in the first line:
var testTest = function(answer) {
and I don't think you think that this would compare anything here (or do you?).
The question remains though, why does = in if (answer = "doggies") "not work"?
An assignment is an expression. The result of that expression is the value that was assigned. Here, the result of answer = "doggies" is "doggies", i.e. you essentially running if ("doggies").
JavaScript performs type coercion. That means it automatically converts values of one data type to values of a different data type if necessary, according to specific rules.
The condition of an if statement has to resolve to a Boolean value. But here you are using a string value as condition. The String -> Boolean conversion rules are pretty simple:
The empty string converts to false.
A non-empty string converts to true.
So, after type conversion, the statement is equivalent to if (true), hence it will always execute the first block, never the else block.

JavaScript: what is the meaning of "!!"? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Can someone explain this 'double negative' trick? [duplicate]
(9 answers)
Closed 8 years ago.
I just ran into this
var strict = !! argStrict;
I can't help but wonder, what is the meaning of !!? Is it a double negative? Seems pretty redundant, not likely that the guys from php.js would use that in such case?
source: http://phpjs.org/functions/in_array/
It forces the type to become a true boolean value rather than a "truthy" value.
Examples:
var a = (1 === true) // comes out as false because 1 and true are different types and not exactly the same
var b = ((!!1) === true) // comes out as true because the 1 is first converted to a boolean value by means of negation (becomes false) and then negated a second time (becomes true)
It means basically "convert to boolean".
It's negating it twice, so it argStrict is "falsy", then !argStrict is true, and !!argStrict is false.
It returns a boolean value. false for undefined, null, 0, '' and true any truthy value.
This is an example of slacker parsing.
If you have a variable, for example a string, and you want to convert it into a boolean, you could do this:
if (myString) {
result = true;
}
This says, if myString is NOT undefined, null, empty, the number 0, the boolean false then set my string to the boolean value to true...
But it is faster, and way cooler to double bang it:
result = !! myString;
Other examples include....
//Convert to number
result = +myString;
//Bang Bang Wiggle - coerces an indexOf check into a boolean
result !!~myString.indexOf('e');

Change NaN to 0 in Javascript [duplicate]

This question already has answers here:
Convert NaN to 0 in JavaScript
(11 answers)
Closed 6 years ago.
I have a code that return to me result of javascript average function in bootstrap modal. But when one of inputs are blank, he return NaN . How i can change NaN to 0?
Here is my code that return result:
$(".average").click(function () {
var result = average();
$("#myModal .modal-body h2").text(result);
$("#myModal").modal();
});
You can check whether a value is NaN using the isNaN function:
var result = average();
if (isNaN(result)) result = 0;
In the future, when ECMAScript 6 is widely available one may consider switching to Number.isNaN, as isNaN does not properly handle strings as parameters.
In comparison to the global isNaN function, Number.isNaN doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN. This also means that only values of the type number, that are also NaN, return true.
Compare:
isNaN("a"); // true
Number.isNaN("a"); // false
Why not just OR, this is one of the most common ways to set a "fallback" value
var result = average() || 0;
as null, undefined and NaN are all falsy, you'd end up with 0, just be aware that 0 is also falsy and would in this case also return 0, which shouldn't be an issue.
var result = average();
result = (isNaN(result) ? 0 : result); //ternary operator check the number is NaN if so reset the result to 0
Use isNaN function.

What it exactly means in Javascript (assigning variable) [duplicate]

This question already has answers here:
Is there a "null coalescing" operator in JavaScript?
(19 answers)
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
Closed 9 years ago.
I have question because I am not sure and cannot find answer on Stack Overflow about this.
What this exactly mean:
variable = variable || {}
or something that:
this.pointX = options.pointX || 6;
I understand that it assign to variable a variable if it exist or empty Object if variable doesn't exist but why it working that?
Is || not mean 'or' here?
The || is effectively working like a SQL COALESCE statement.
var x = y || z;
means:
if y evaluates to a "truthy" value, assign y to x.
if y evaluates to a "falsy" value, assign z to x.
See http://11heavens.com/falsy-and-truthy-in-javascript for more detail on "truthy/falsy" (or just google it).
The || is an or operator.
It basically means if variable is undefined, it will assign variable to a new object literal.
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Expressions_and_Operators#Logical_operators
|| does mean OR here:
var x = 5
var x = x || {} //If v is defined, v = v, else v = {} (new, empty, object).
//x = 5 since x already was defined
var y = y || {}
//y = {} since y was undefined, the second part is run.
The || operator returns the actual object that determines its "truthiness" value, not just a boolean (true or false). It "short circuits" in that once it can determine the result, it stops.
If variable has a truthiness value of true, it is returned (since when true is ored with anything, the result is true). Otherwise, the second operand is returned (even if it has a truthiness value of false) since it determines the truthiness of the whole expression.
this.pointX = options.pointX || 6;
Means assign this.pointX the value of options.pointX if available(i.e. not null) otherwise assign the value of 6
The || operator in JavaScript differs from some other languages you'll find it in. When JavaScript evaluates || it seems to return one operand OR the other. It doesn't do a typical truth table evaluation evaluating to true if any operand evaluates to true, and false if not.

Categories