Result of syntax in JS [duplicate] - javascript

This question already has answers here:
How is this valid javascript, and what purpose does it serve?
(2 answers)
Closed 7 years ago.
I'm switching from C++ to JS.
//if ( condition )
{
instruction1;
instruction2;
}
In C/C++ commenting if before the block would simply execute the block unconditionally.
Could I use it the same way in JS? Or would it create an unnamed object which is never used?

A very quick way of testing:
{
alert("foo");
}
http://jsfiddle.net/4obksb0s/
Yes - it appears to run fine.

Related

Cannot understand this javascript statement [duplicate]

This question already has answers here:
Javascript || operator
(5 answers)
Javascript Shorthand - What Does the '||' Operator Mean When Used in an Assignment? [duplicate]
(6 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I've been given a function by a client to create a custom script for tracking purposes which is fine, but being new to JS I'm having trouble understanding this line of code:
d[p]=d[p]||function(){(d[p].q=d[p].q||[]).push(arguments);};
function example is:
(function (d,e,k,u,p) {
d[p]=d[p]||function(){(d[p].q=d[p].q||[]).push(arguments);};
var a=e.createElement(k),b=e.getElementsByTagName(k)[0]; a.async=1;a.src=u;b.parentNode.insertBefore(a, b);
}(window,document,'script','https://www.example.com/bundle.js','stringname'));
If anyone could explain it that would be great
Just want to understand the statement better, it works fine and I can copy and paste with the correct url and string name but better if I understand the statement

what is the logic behind this logical expression [duplicate]

This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 8 months ago.
function expect(argument) {
this.eat(argument) || this.undefined();
}
Can someone please help me explain how this code works... the question is focused on this.eat(argument) || this.undefined();. I am guessing that one of those methods would run? and if it is, how do I tell which would run
Or Condition || will return the first true value.
if this.eat(argument) return true or any value which can be converted to true value, So the this.undefined() function will not execute.
If not it will execute.

Advantage of inline boolean conditional vs explicit if statement? [duplicate]

This question already has answers here:
What is "x && foo()"?
(5 answers)
Is <boolean expression> && statement() the same as if(<boolean expression>) statement()?
(5 answers)
Closed 10 months ago.
Recently I came across this little bit of JS logic:
close(focusAfter) {
if (! this.open) return;
this.open = false;
focusAfter && focusAfter.focus();
},
The truthy-ness check of focusAfter is clearly used as an inline conditional to make sure a valid object/element exists before calling .focus() on it.
It seems to me that the equivalent logic could be clarified when rewritten as:
if (focusAfter) focusAfter.focus();
My question is: is there any "not-immediately-obvious" advantage to the inline boolean-check-then-execution shown in the original example versus explicitly writing an inline if statement to "wrap" the executed logic (in this case, focusAfter.focus())?

How do you write the opposite of .classList.contains("class"), meaning classList does not conatin [duplicate]

This question already has answers here:
How to negate code in "if" statement block in JavaScript -JQuery like 'if not then..'
(2 answers)
Closed 2 years ago.
I was wondering how to write the opposite of this.classList.contains("theClass") meaning this.classList.does not contains("theClass"). This is for an if statement. In my case it is
if (square.classList.contains("bitCoin")){
jump();
}
but I want to make it if it DOES NOT contain class "bitCoin".
Thanks
if (!square.classList.contains("bitCoin"))

jQuery global variable, not working [duplicate]

This question already has answers here:
What does $(function() {} ); do?
(6 answers)
Closed 4 years ago.
Why is this simple global variable not working inside a function
var test_1 = "None";
$(function(){
console.log(test_1)
})
I am getting the result Undefined
Check, whether you have jquery or not. I have created a jsfiddle (https://jsfiddle.net/2kudvsr0/) for your code and its printing None (desired output).
Just make sure you are executing your code once jQuery is imported successfully.

Categories