8

In default 'if' one line statement are two block, for true, and false:

variable ? true block : false block;

How declare 'if' with one block?

I expect something like this:

variable ? true block;
Emerceen
  • 2,735
  • 3
  • 17
  • 22

2 Answers2

11
if(variable) block;

With the conditional operator you need the false bit also.

taguenizy
  • 2,140
  • 1
  • 8
  • 26
  • is it right to pass just `null` for the false block, when we use the ternary? – Ceylan Mumun Kocabaş Sep 14 '16 at 10:47
  • 1
    You could. I wouldn't use it. I use ternary when I know I have 2 operations. If just care about the `true` or `false` condition I would use it this way. It's more a preference then anything @CeylanMumunKocabaş – taguenizy Sep 14 '16 at 10:51
  • 1
    @CeylanMumunKocabaş—you can do that, but it's more to type and not as clear as using a simple *if*. – RobG Sep 14 '16 at 11:02
4

You can do:

variable && block

For example:

let variable = true;
let o = variable && 3;
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299