Came across weird javascript operation [duplicate] - javascript

This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 8 years ago.
I am working on a project, designed by someone else. I came across the following operation. I have no idea what it is doing. It seems to be returning 1.
Anyone care to elaborate?
Thank you!
( 7 > 8?2:1)

You're looking at the Ternary Operator.
It consists of (condition) ? (expression1) : (expression2). The entire expression will evaluate to (expression1) if (condition) is true, and (expression2) if (condition) is false.
var i = (7 > 8 ? 2 : 1);
translates into
if (7 > 8)
{
i = 2;
}
else
{
i = 1;
}

See: http://en.wikipedia.org/wiki/%3F:
Your example would return 2 if 7 > 8, or 1 otherwise.

? : is a ternary operator. This is equivalent to
var x = 0;
if (7 > 8){
x = 2;
} else {
x = 1;
}
It is a terse way of expressing simple conditional statements. It is a great way to conditionally assign values to a variable without the verbose semantics used above.

Related

Will if / else if / else become an expressions in typescript / javascript at some point?

I really like this feature in Kotlin:
In Kotlin, if is an expression, i.e. it returns a value.
// As expression
val max = if (a > b) a else b
(from https://kotlinlang.org/docs/reference/control-flow.html)
Is this also planned for typescript / javascript?
Update:
In reply to the ternary operator suggestions, of course I also mean cases with else if:
val bla = if ( 5 == 6) {
7
} else if (5 == 7) {
8
} else {
9
}
println(bla) // prints 9
Is this also planned for typescript / javascript?
No. Statements vs. Only Expressions is a pretty hard fork in language design and will not change without breaking existing code so will not happen.
Simple example:
var foo = 123;
function inc(){
foo = foo + 1;
}
Going down the expression route would change the return of this function from undefined to foo which can drastically break existing code for more complex functions.
It already exists Ternary Conditional

The kind of jQuery selector [duplicate]

This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 6 years ago.
I have seen jQuery code's like this
return 13 == t.keyCode ? (t.preventDefault(), !1) : void 0
What that mean for ? and : ?
Please give me reference, because I still newbie in jQuery. Thank's a lot
Its ternary operator.
Shorthand for writting if/else
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It is a shorthand for if else:
Translation:
if(13 == t.keyCode) { return (t.preventDefault(), !1); } else { return void 0; }

Bit Pattern in JavaScript [duplicate]

This question already has answers here:
String Conversion in Javascript (Decimal to Binary)
(5 answers)
Closed 7 years ago.
I would like to ask help whether am I doing the right thing or not. You see I am trying to test myself by displaying the bit pattern of a number in the most efficient way as possible. But I'm having trouble on how to display the pattern cause I'm still learning javascript. Here's my code.
<script>
var bitPattern = function(given) {
for(var i = 1 << 31; i > 0; i = i / 2){
document.write((given & i) ? 1 : 0);
}
};
var number = prompt("Enter a number to convert: ");
bitPattern(number);
</script>
The best way to do this is:
var number = prompt("Enter a number to convert: ");
var bitPattern = parseInt(number).toString(2);
document.write(bitPattern);

javascript syntax for ? and : [duplicate]

This question already has answers here:
JavaScript ternary operator example with functions
(6 answers)
What does the question mark and the colon (?: ternary operator) mean in objective-c?
(13 answers)
Closed 8 years ago.
I have two questions:
1. I have a JavaScript function code:
var firstOrNull = function (elems){
return (elems.length > 0 ) ? elems[0] : null;
}
What does ? and : means in this code?
2. What is the meaning of this code:
var stopEvent = function(event){ event.stopPropagation() }
This is Conditional Operator
stopPropagation method of javascript event. It uses to prevent further propagation of the current event.
? and : pair indicate ternary operator in Javascript.
(elems.length > 0 ) ? elems[0] : null; line means if elems length is greater than zero then return elems[0] otherwise return null.
This is called the ternary operators
if(elements.length > 0){
return elems[0];
} else {
return null;
}
is equivalent to :
return (elems.length > 0 ) ? elems[0] : null;
ternary operators

what does the "?" and ":" sign mean in a line of java code? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a Question Mark “?” and Colon “:” Operator Used for?
Question mark in java code
I am writing codes for a RBG to HSV converter.
I have this line:
var d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
i dont really understand what the "?" and the ":" means here.
This is the short way to make a condition :
Condition ? Statment1 : Statement2;
Means
If (Condition) {Statement1} else {Statement2}
Its Ternary Operator:
C = condition? A : B
is equivalent to
if (condition){
C= A;
} else{
C= B;
}
It also support nesting i.e. C = condition1? A : condition2?D:E, which is equivalent to
if (condition1){
C=  A;
} else if (condition2){
C=  D;
} else{
C= E;
}
This is called ternary operator in java.
Based on java tutorial
Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands.
If first expression results in true, then assign second operand as value, otherwise third operand as value.
It means
if (r==minRGB)
d = g-b
else
if(b==minRGB)
d=r-g
else
d=b-r
In c-based languages, it means:
? :
Its short-hand for an if-else, basically.
it work similar to if than else
if (r==minRGB)
d = g-b;
}else{
if (b==minRGB)
{
d = r-g;
}else{
d = b-r;
}
}
Its called ternary operator (?:): -
System.out.println(condition? value1 : value2);
The above expression is evaluated like: -
if (condition) {
System.out.println(value1);
} else {
System.out.println(value2);
}

Categories