JavaScript Extended Ternary expression

I know you can do ternary expressions in Javascript for an if - else statement, but how about an else- else if- else statement? I thought that surely this would be supported but I haven't been able to find any info about it and wasn't able to get it to work just hacking around.

Answer:1

In contrast to Robby Cornelissen's answer - there is no problems with readability if you format it properly (and not writing PHP, since it messed up the operator by making it left-associative in contrast to all other languages that have that construct):

var y =
  x == 0 ? "zero" :
  x == 1 ? "one" :
  "other";

EDIT

What I was looking for is a shorter version of "if expression 1 is true, return expression 1. Else if expression 2 is true, return expression 2. Else return expression 3". Is there no clean way to do this?

There is: expression1 || expression2 || expression3. (It would have been nice if you had put this into your question in the first place.) This is commonly used for default values:

var defaults = null;
function hello(name) {
  var displayName = name || (defaults && defaults.name) || "Anonymous";
  console.log("Hello, " + displayName + ".");
}

hello("George");
// => Hello, George.
hello();
// => Hello, Anonymous.
defaults = {};
hello();
// => Hello, Anonymous.
defaults.name = "You"
hello();
// => Hello, You.

However, it is important to be aware of the conditions for truthiness. For example, if you expect "" or 0 to be a valid value that does not need to be replaced by a default, the code will fail; this trick only works when the set of possible non-default values is exactly the set of truthy values, no more and no less. E.g.

function increment(val, by) {
    return val + (by || 1);      // BUG
}

increment(10, 4);
// => 14
increment(10, 1);
// => 11
increment(10);
// => 11
increment(10, 0);
// => 11                         <-- should be 10

In this case you need to be explicit:

function increment(val, by) {
    return val + (typeof(by) === "undefined" ? 1 : by);
}
Answer:2

I wouldn't recommend it because of readability, but you could just nest ternary operators:

var y = (x == 0 ? "zero" : (x == 1 ? "one" : "other"));

This would be the equivalent of:

var y;

if (x == 0) {
    y = "zero";
} else if (x == 1) {
    y = "one";
} else {
    y = "other";
}
Answer:3

Being fairly new to jquery and javascript I can't seem to understand how the .text() method really works. I read through the jQuery documentation but still can't figure it out. for (var i=0; i < ...

Being fairly new to jquery and javascript I can't seem to understand how the .text() method really works. I read through the jQuery documentation but still can't figure it out. for (var i=0; i < ...

  1. jquery text method javascript equivalent
  2. jquery text method not working
  3. jquery text method new line
  4. jquery get text method

When adding two or more functions in my jquery script file the function/s stops working. It's working perfectly fine when I've only written one function. But when I'm adding a second one both of ...

When adding two or more functions in my jquery script file the function/s stops working. It's working perfectly fine when I've only written one function. But when I'm adding a second one both of ...

  1. break jquery functions

Calling this async Web Method, I get a 500 error. Are there other ways to call a web method async? Stacktrace: Server Error in '/' Application. Unknown web method SendMessage. Parameter name: ...

Calling this async Web Method, I get a 500 error. Are there other ways to call a web method async? Stacktrace: Server Error in '/' Application. Unknown web method SendMessage. Parameter name: ...

  1. call method async c#
  2. call method async java
  3. call method async
  4. call async method without await
  5. call async method synchronously c#
  6. call async method without await c#
  7. call async method from main
  8. call async method from main c#
  9. call async method in constructor c#
  10. call async method in constructor
  11. call async method from sync c#
  12. call async method javascript
  13. call async method synchronously
  14. call async method from sync
  15. call async method in parallel.foreach
  16. call async method from ajax c#
  17. call async method without await javascript
  18. call async method from console app c#
  19. call async method in button click c#
  20. call async method from static main

Why I ask an easy question: Hi, lots of people asked how to split an string in javascript by multiple values, but I don't understand how it works the /[^\w\s]|_/ in the split asked, so I don't know ...

Why I ask an easy question: Hi, lots of people asked how to split an string in javascript by multiple values, but I don't understand how it works the /[^\w\s]|_/ in the split asked, so I don't know ...

  1. split string carriage return javascript
  2. split string carriage return c#
  3. string split carriage return
  4. python split string carriage return
  5. powershell split string carriage return
  6. sql split string carriage return
  7. excel split string carriage return
  8. vba split string carriage return
  9. php split string carriage return
  10. java split string carriage return
  11. oracle split string carriage return
  12. bash split string carriage return
  13. ruby split string carriage return
  14. perl split string carriage return
  15. vbscript split string carriage return
  16. excel vba split string carriage return
  17. vb.net split string carriage return
  18. c sharp split string carriage return
  19. typescript split string by carriage return
  20. jquery split string by carriage return