A weird while loop syntax [duplicate] - javascript

This question already has answers here:
Please help me understand the "while" loop in this JavaScript snippet
(3 answers)
Closed 8 years ago.
this code: http://ajaxian.com/archives/attack-of-the-ie-conditional-comment
var ie = (function(){
var undef, v = 3, div = document.createElement('div');
while (
div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i>< ![endif]-->',
div.getElementsByTagName('i')[0]
);
return v > 4 ? v : undef;
}());
Could you please explain to me why this while loop works? Is that even correct syntax?
Edit: I do understand what this code is trying to do, but the syntax is just not obvious to me..

It works because the first statement gets executed and due to comma operator, only the second statement is considered for evaluation, which will evaluate to true or false.

This while loop is correct, because in while statements are seperated by , so the last statement's value will be used as condition of while.
This while loop will finish whenever div.getElementsByTagName('i')[0] will be null or undefined.

Related

Checking value with JavaScript [duplicate]

This question already has answers here:
How does the single equal sign work in the if statement in javascript
(6 answers)
Closed 3 years ago.
This is my general code (in JavaScript):
let x = Math.floor(Math.random()×6)+1);
if (x=1){
do this
} else if (x=2){
do that
} else if.... and so on.
Whenever I test this code in a browser, only actions that could happen in the {do this} section happen. x is being locked as 1, even though it is meant to be a fair 6 sided die. Any ideas why this is happening, and how I can fix it?
The = operator in JavaScript is for assignment. x=1 will always return true because that is an assignment that will never fail. To test for equality use == (equality with conversion) or === (strict equality).
You have your if (x=1) assigning the value of 1 to x, rather than checking if it's equal to it. This results in it always returning TRUE. Using a pair of == (or ===) will solve this.

What is assignment with round brackets used for in JavaScript? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 3 years ago.
In one quiz I came across the following question:
What is the value of val?
var val = (5, 10, 15);
Despite the fact that I'm quite experienced with JavaScript I have never seen such kind of assignment before and failed to find any information about it on the internet.
What are the use cases and how it actually works?
Thanks in advance.
In JS, the comma operator (,) evaluates all operands, and returns the last.
So, in your case, it is equivalent to:
var val=15;
The 5 and 10 are evaluated and then silently dropped.
But consider the following:
var i=0;
var val=(i++,15);
console.log(i) //1
console.log(val) //15
The value of i is increased by 1, and it returns 15, combined to a single expression.

What is the Javascript equivalent of writing 'If not" [duplicate]

This question already has answers here:
Best way to find if an item is in a JavaScript array? [duplicate]
(8 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 6 years ago.
Python programmer here.
I don't know how to write this. I tried using 'if !in' and '!if in', but I don't know how. Tried to Google it but got no results.
The correct syntax is
if(!condition){
expression();
}
Note that you need parenthesis around the condition.
#plalx wants a formal definition, and here you go:
IfStatement:
if(Expression) Statement else Statement
if(Expression) Statement
In case of any ambiguity the else would be matched with the nearest if.
If you have some value:
var excludeMe = "something unwanted";
Then you can use the following if statement:
if(myTestCase !== excludeMe) { //do something...}
Keep in mind that != does not check type and !== does check type. So, 1 != "1" is false and 1 !== "1" is true.

Please interpret this java script line of code [duplicate]

This question already has answers here:
What is "?:" notation in JavaScript?
(5 answers)
Closed 6 years ago.
Can someone interpret this javascript line for me?
mouseWheelEventName = $.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel',
Need to know what "?" does, and what 'DOMMouseScroll' : 'mousewheel', is saying particularly the "," at the end of the line... why isn't it a ";"
Thank you.
This is a ternary operator, used as a shorthand conditional statement:
it's the same as saying:
if ($.browser.mozilla) {
mouseWheelEventName = 'DOMMouseScroll';
} else {
mouseWheelEventName = 'mousewheel';
}
The first piece before the = is declaring a variable (mouseWheelEventName) dependent on the following condition.
The next piece between = the ? is the condition (is $.browser.mozilla true?).
Immediately after the ? is the then portion (if the condition is true, set the variable mouseWheelEventName to the string DOMMouseScroll).
After the : is the else (if the condition is NOT true, set the variable mouseWheelEventName to the string mousewheel).
Further reading:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
As for why there is a comma at the end of it, we'd need to see a more complete code sample including what follows that to say for certain. If I had to guess, I would say the author of the code was chaining variable statements. This answer may shed some light for you: Javascript best practices, why to use comma to chain function/variable declarations? (see chosen answer)

What do ? and : mean in javascript [duplicate]

This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
What does ? : mean in Javascript? [duplicate]
(9 answers)
Closed 10 years ago.
I am trying to understand the Javascript/jQuery in this http://www.queness.com/post/12078/create-jquery-pinterest-pin-it-plugin plugin. The lines 20 and 22 are confusing me, the code is:
pi_media = e.data('media') ? e.data('media') : e[0].src,
pi_desc = e.attr('title') ? e.attr('title') : e.attr('alt'),
Can anyone help me out with what these lines mean in Javascript
It's the JavaScript ternary operator.
x = condition ? a : b
is equivalent to
if(condition)
x = a;
else
x = b;
Note that an assignment is not necessary. As an expression, it simply evaluates and produces a or b depending on the truth value of condition.
It's called the Ternary Operator. It means:
Evaluate the expression to the left of the ?
If the expression evaluated to true, run the first piece of code (between the ? and the :)
If the expression evaluated to false, run the second piece of code (after the :)
This is a construct common to many C-style languages.
Those are part of the ternary operator.
Basically, if the condition before the ? is evaluated to true, the expression immediately following the ? is the one which is evaluated, and otherwise the expression following the : is evaluated.
For example take the code:
var result=condition?arg1:arg2;
First the condition is evaluated.
If the evaluation results to true, then arg1 is returned and assigned to result
If the evaluation results to false, then arg2 is returned and assigned to result

Categories