Javascript var = int || int? [duplicate] - javascript

This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 5 years ago.
Little question, maybe it's a basic one .... but
What should be the value of ub ?
Suppose that getuploadedBytes return an Int,
the value of ub should suppose to be what ?
The min or max value between method and right operator ?
ub = that._getUploadedBytes(jqXHR) || (ub + o.chunkSize);

In summary, if that._getUploadedBytes(jqXHR) is non-zero, that value will be returned, otherwise (ub + o.chunkSize) is returned.
The || operator prefers the left value if it is truthy, and the right value otherwise.
Assuming that that._getUploadedBytes(jqXHR) returns an integer, then the only integer that is falsey is 0.

The value will be the result of _getUploadedBytes unless that function returns 0, in which case it will be ub + o.chunkSize.
0 is a falsy value, which means the condition will fail and the other calculation will produce the result.

Related

What does JavaScript do with Number(undefined)? [duplicate]

This question already has answers here:
Why is NaN === NaN false? [duplicate]
(3 answers)
Closed 7 years ago.
I recently thought about defining a function to copy the functionality of isNaN out of boredom when I found out, that Number(undefined) equals NaN</code>, but if you doNumber(undefined) === NaNyou getfalse`.
I even tried (typeof Number(undefined)) === (typeof NaN) which returns true.
What is JavaScript doing here?
The constant NaN is never equal to anything, including NaN. The value of typeof NaN is "number", because NaN is a number constant.
The value of Number(undefined) is NaN. You can use isNaN() to verify that, or simply:
var x = Number(undefined);
if (x !== x) alert("It's NaN!");

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.

Javascript idiom: What does if (x === +x) do? [duplicate]

This question already has answers here:
+ operator before expression in javascript: what does it do?
(4 answers)
Closed 10 years ago.
Reading through the source code of underscore.js I stumbled upon the following line:
... if (obj.length === +obj.length) { ...
That's a bit confusing for me. What is actually being compared here? I believe it has something to do about detecting native arrays, but cannot figure out what's actually going on. What does the + do? Why use === instead of ==? And what are the performance benefits of this style?
The + coerces the value to an Number (much like !! coerces it to a boolean).
if (x === +x)
...can be used to confirm that x itself contains an integer value. In this case it may be to make sure that the length property of obj is an integer and has not been overwritten by a string value, as that can screw up iteration if obj is treated as an array.
It is a silly (IMO) way of checking if obj.length is a Number. This is better:
typeof obj.length == "number"
The + coheres what is on the right side to be a number.
In this case if length was not a property on the object undefined would be returned. + undefined will yield Nan and this evalutation be false.
If the string can be coheres-ed into a number then it will be.. e.g + '1' will yield 1 as a Number this is especially important when dealing with hex values in string form e.g. +'0x7070' yields 28784

Categories