0

I found some code from https://github.com/andrewgodwin

    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";

What does 'something' ? 'something' : 'something' means?

touchingtwist
  • 1,496
  • 3
  • 13
  • 30
  • 1
    `?:` is the ternary operator. It is a short and often gorgeous method of writing a simple `if` statement in many languages. – jdgregson Feb 10 '18 at 07:22

3 Answers3

2

It is conditional operator in javascript ref. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

programtreasures
  • 4,104
  • 1
  • 7
  • 25
2
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";

that means:

var ws_scheme;
if (window.location.protocol == "https:") {
    ws_scheme = "wss";
} else {
    ws_scheme = "ws";
}
xianshenglu
  • 4,009
  • 1
  • 10
  • 25
2

It's called conditional (ternary) operator. This operator is frequently used as a shortcut for the if statement. If the condition before the "?" is true then the value just after the "?" is assigned to the variable, else the value after ":" is assigned to the variable.

Details at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Ari Singh
  • 1,070
  • 5
  • 11