This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 4 years ago.
I brow some code in javascript, found some code of function like that
function isPromise(obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}
As what I say to the title, why this function use !!obj" ??
If you are referring to "double bang" in JavaScript it is a force coercion falsely or truthy to its boolean true or false
See this answer:
What is the !! (not not) operator in JavaScript?
Related
This question already has answers here:
How do you test for NaN in JavaScript?
(3 answers)
Can (a== 1 && a ==2 && a==3) ever evaluate to true?
(29 answers)
Closed 3 months ago.
I was reading the source code of core-js and I saw the following:
if (value != value) return true;
What does it actually mean? When exactly value won't be equal to itself?
It could be if:
value is NaN
console.log(NaN != NaN);
value is actually a getter on the global object, and it returns something different each time:
let i = 0;
Object.defineProperty(window, 'value', { get: () => i++ });
console.log(value != value);
This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 2 years ago.
I currently have this JavaScript function lying around within my code:
getCoverPhoto(item) {
if (item != undefined && item.gallery != undefined && item.gallery[0] != undefined)
return item.gallery[0].media;
return "";
}
How can I simplify the if condition above? And if it can't be simplified, can it be written in a better way?
For example with ternary operator:
getCoverPhoto(item) {
return item && item.gallery && item.gallery[0] ? item.gallery[0].media : '';
}
Read further in the documentation:
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as a shortcut for the if statement.
Or with ES6+:
const getCoverPhoto = item => item && item.gallery && item.gallery[0] ? item.gallery[0].media : '';
I hope that helps!
Here is a more simplified version of your code.
getCoverPhoto(item) {
if (item && item.gallery && item.gallery[0])
return item.gallery[0].media;
return "";
}
Use destructing to reduce the conditions. Below code should work for you.
getCoverPhoto(item) {
const { gallery = [] }= item; // this is destructing with default value assignment.
return item.gallery[0] ? item.gallery[0].media : '';
}
This question already has answers here:
Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?
(22 answers)
Closed 4 years ago.
I'm wondering if there's a shorthand way to write something like this in JavaScript
if (thingExists) {
thingExists.manipulate
}
I think I remember seeing something along the lines of
thingExists?.manipulate
but that may have been TypeScript or something.
Anyway, any knowledge on that matter is appreciated,
Thanks!
You can use && for short circuiting:
thingExists && thingExists.manipulate
thingExists.manipulate will be evaluated if and only if thingExists is truthy.
Example:
var obj = { func: function() { console.log("func is called"); } };
obj && obj.func(); // => func is called
var falsy = null;
falsy && falsy.imaginaryFunc(); // => nothing
This question already has answers here:
What is "x && foo()"?
(5 answers)
What does this symbol mean in JavaScript?
(1 answer)
What a strange syntax?
(3 answers)
Closed 4 years ago.
jBox.prototype.position = function (options)
{
// this line
!options && (options = {});
}
In conventional programming boolean statements are used in if else statements.
What does !options && (options = {}); translate too?
options is a json or array. What does !options mean?
options = {} is assigning a empty json to variable options, how does it return a boolean value to be used with &&.
The code:
!options && (options = {});
is the equivalent of:
if(!options) {
options = {};
}
It means that if options is a falsy value (empty) then initialize it with an empty object literal.
This question already has answers here:
Why does isNaN(" ") (string with spaces) equal false?
(25 answers)
Closed 8 years ago.
I'm trying to write a simple test for the input of a function to determine if all of the inputs are numbers or not.
function numbers(){
for (var i = 0; i < arguments.length; i++) {
if (isNaN(arguments[i])) return false;
}
return true;
}
However, when I pass in a list of numbers as characters (eg. numbers("1", "2")) I get true instead of the expected false.
isNaN implicitly coerces the argument to Number, and then checks whether this coerced value is NaN.
See http://es5.github.io/#x15.1.2.4
That is, isNaN(foo) is equivalent to isNaN(Number(foo))
Code fix:
if (typeof arguments[i] !== 'number' || isNaN(arguments[i])) return false;
The second part of the condition is because typeof NaN === 'number'.
Your function might be a bit more readable in functional style, using ES5's Array#every method:
//returns whether all arguments are of type Number and not NaN
function numbers() {
return [].every.call(arguments, function(arg) {
return typeof arg === 'number' && !isNaN(arg);
});
}
isNan() will attempt to cast it to a number, and then check. I would use this instead
if(!isNaN(parseFloat(arguments[i])) && isFinite(arguments[i])){
return false;
}
Alternatively, if you're using jQuery, you could use its built-in $.isNumeric() function