Questions tagged [shorthand-if]

47 questions
41
votes
7 answers

javascript shorthand if statement, without the else portion

So I'm using a shorthand javascript if/else statement (I read somewhere they're called Ternary statements?) this.dragHandle.hasClass('handle-low') ? direction = "left" : direction = "right" This works great, but what if later I want to use just a…
ahren
  • 15,967
  • 5
  • 44
  • 67
10
votes
2 answers

shorthand php if{} ELSE IF{} else{}

is there a possibility to write a shorthand if, ELSE IF, else statement for php. if / else is clear but is there a shorthanded way when i want to use elseif too (except switch)? if ($typeArray == 'date'){ echo 'date'; } else if($typeArray ==…
Timotheus Triebl
  • 1,466
  • 3
  • 18
  • 27
8
votes
3 answers

JavaScript Design Patterns Help Needed: Loose Augmentation of Modules

Edit for clarity - @Qantas94Heavy - I understand what it is "saying" or supposed to do, what I don't understand is why & more importantly how it works: I was reading an advanced tutorial on the JS Module Pattern, and it gave this example: var MODULE…
6
votes
5 answers

c# If else shorthand

In c# can I do something like this in a shorthand? bool validName = true; if (validName) { name = "Daniel"; surname = "Smith"; } else { MessageBox.Show("Invalid name"); } I was just wondering if something similar to this would…
adminSoftDK
  • 1,852
  • 16
  • 37
6
votes
6 answers

Is there JS shorthand for conditional "not equal to a and not equal to b" and such?

I was just wondering if there is some JS shorthand for something like this: if (x != 1 && x != 2) do stuff; Does such a beast exist? Instead, I want to say something like this: if (x != 1:2) do stuff;
4
votes
2 answers

Ternary operator when casting a variable

While writing is_numeric($var) ? (Int)$var : (String)$var;, I was wondering if it could be possible to move the ternary operator to the part where I cast the variable: echo (is_numeric($var) ? Int : String)$var; Not to my surprise, it didn't…
Bas Peeters
  • 3,085
  • 4
  • 30
  • 47
4
votes
3 answers

Shorthand if then else performance/optimization in php

Talking about PHP, i would like to ask if there is a difference in performance between these two: $name=($IsBoy)?"George":"Mary"; vs if($IsBoy) { $name="George"; } else { $name="Mary"; } Will these two result to different opcode? If yes,…
Sharky
  • 5,671
  • 3
  • 35
  • 66
3
votes
4 answers

How to write in shorthand form if / else if / else?

Is there a shorthand for an if / else if / else statement? For example, I know there's one for an if / else statement: var n = $("#example div").length; $("body").css("background", (n < 2) ? "green" : "orange"); But how do I write the following in…
GTS Joe
  • 2,180
  • 6
  • 33
  • 59
3
votes
1 answer

String check with if js shorthand

I have var names = []; and want push there some string only if it's not empty. Is there way make it with some shorthand method in js?) That i have now. if ("" != opportunityName) { names.push(opportunityName); } And that Don't wotk…
Maks Martynov
  • 440
  • 3
  • 18
2
votes
2 answers

Multiple statements if condition is true in shorthand if

I recently discovered the shorthand if statement and after searching online I couldn't find a definite answer. Is it possible to execute 2 statements if the condition is true/false? int x = (expression) ? 1 : 2; for example int x = (expression) ?…
Patrick
  • 333
  • 1
  • 4
  • 15
2
votes
1 answer

Does c# have a shorthand "something if (condition);" statement?

Simple and short: does C# have a shorthand if statement similar to Ruby's? return if (condition)
jj_
  • 2,548
  • 28
  • 55
2
votes
2 answers

Not sure what the value of a is

where: $b = true; $c = 0; $a = ($a ? ($a ? $b : $c) : ($c ? $a : $b)); I'm not sure how to work out a. So I understand that this is a shorthand operator, and usually it's a case of: $value ? true : false meaning if $a = true { true } else { false…
2
votes
1 answer

php if/else shorthand fail

Normal expression works fine, shorthand doesn't. Where am I wrong here? if (isset($var)) $value = $var; elseif ($str !== 'string') $value = $str; else $value = null; // works just fine $value = (isset($var)) ? $var : ($str !== 'string') ? $str :…
nxet
  • 721
  • 1
  • 8
  • 21
2
votes
2 answers

Value variable as 0 not as false in shorthand-if expression

I'm calling a function in either those two ways foo([x,y]) or foo({x:x,y:y}) with x,y ∈ [0,∞) the foo function looks like this var x = pos.x || pos[0], y = pos.y || pos[1]; If I call the function the second way with x=0 then pos.x will be…
InsOp
  • 1,446
  • 2
  • 19
  • 31
2
votes
3 answers

Alternate for shorthand if else

I have seen a lot of people being used to do this in their code: $value = isset($text) ? $text : ""; This code essentially means set $value to $number(if it is set) else set $value to "" I experimented a bit and came up with a much cleaner…
Noman Ur Rehman
  • 5,973
  • 2
  • 21
  • 35
1
2 3 4