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.
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:
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:
How do I test for an empty JavaScript object?
(48 answers)
Closed 4 years ago.
How do I check for an empty JSON object in Javascript? I thought the key was
var isEmpty = (response || []).length === 0;
but this isn't it. In my JS console, I have tried
jsonresp = {}
{}
jsonresp.length
undefined
What's driving this is I have a Ruby on Rails controller that is returning the following
render json: job_id.eql?(cur_job_id) ? {} : json_obj
In the case where the controller returns the "{}" is where I'm having trouble on the JS side recognizing if it is empty or not.
You can also try:
if (typeof jsonresp == 'undefined' || typeof jsonresp == 'null'){
// some logic here
}
You can use Object.keys() function to get keys of the object as an array and then check the array to see whether it is empty.
var obj = {};
var arr = Object.keys(obj);
console.log(arr);
console.log("Is Empty : " + (arr.length == 0));
🏄 Shortly:
const isEmpty = !Object.keys(jsonresp).length
This question already has answers here:
What does the following code mean in javascript? [duplicate]
(2 answers)
Closed 6 years ago.
javaScript(variable)
var s = s || {};
s.c = {};
what purposes it will be use?
var s = s || {};
This means that if s is null, undefined or false (it computes to false), then an empty object {} will be assigned to s, so that the second line would not cause an error.
But this notation is inacurate. It should be something like:
var s = (typeof s == 'object') ? s : {};
because in the first example if s is a number the second line will still cause an error.
In the second example notation A ? B : C; is equal to:
if(A){
B;
}else{
C;
}
This question already has answers here:
Determine original name of variable after its passed to a function
(9 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 7 years ago.
I know this is completely useless, but I'm nonetheless curious. Is it possible to do something like the below?
function getVariableName ( v )
{
// .... do something .....
// return the string "v"
}
var x = 69;
var y = "somestring";
console.log(getVariableName(x)); // prints "x" to the console
console.log(getVariableName(y)); // prints "y" to the console
function getArgNames(fn) {
var matches = fn.toString().match(/\(([a-z_, ]+)\)/i);
if (matches.length > 1) {
return matches[1].replace(/\s+/g, '').split(',');
}
return [];
}
That will return an array of the names of all arguments passed in to the function.