Conditions in Javascript

Asked

Viewed 596 times

2

I recently discovered that it is possible that it is possible to store the result of a condition (such as a if) in a variable:

var x = (1 > 2); // false
var y = (2 > 1); // true

Check out Jsfiddle: http://jsfiddle.net/8ym9z/

  • What’s this resource called?
  • What is it for/when it is useful?
  • Also available in other languages?

3 answers

7


Expressions

We usually call +, - * etc. mathematical operators, <, >, == of conditional operators, or by comparison, and and, or, or &&, || and similar of boolean operators 1.

A set of values, permeated by operators, is what we call "expression", and an expression returns a value.


A "Conditional Expression"

The condition is nothing special, because as said, the > is only one operator. And the if simply uses its result to define what it will do.

When you say:

if (x > y) { ...

You are first calculating the value of x > y. Let’s suppose that by chance x really be bigger than y, the result of this "calculation" shall be the value true, so it would have the same effect as if it were written if (true) in that particular case.

Likewise, if (x > y) is equivalent to:

var meu_teste = x > y; // teste recebe o valor true se x > y

if ( meu_teste ) { ... 

Only after "calculated" the value of x > y that the if will use the returned result. Actually if "doesn’t even know, doesn’t care" what happened inside the parentheses.


The "Boolian Expression" 1

Likewise, or and and could also be stored in var" ;)

var condicoes = true or false; // true || false, etc.
if ( condicoes ) { ...

In short: you’re just saving a value from an operation like any other. Just like a + b, the operations a > b, a || b and a++ return a value at the end.

1. Spelled Boolean in many places, but confirmed in the VOLP that in our language it is with "i"

4

This is available in other languages as well...

The Great difference is that in Javascript you do not need to specify the type of your variable, IE, do not need to inform if it is a Boolean, String, Interger or something like.

Your operation (1 > 2) returns a boolean value (false in this case ) so you can assign this value to your variable x.

In Java for example you will need to say that your variable will receive a Boolean to perform this operation.

public static void main(String[] args) {
    Boolean x = (1 > 2);
    System.out.println(x);
}

1

In php has something similar too, but look at this.

$x = (3 > 1);
$y = (1 > 2);

var_dump($x); //boolean true
var_dump($y); //boolean false

echo $x; //1
echo $y; //(imprime nada)

However this is no special feature, when using a comparison operator, the result will always be a boolean.

  • 3

    This is because of the conversion of Boolean to string that php does in echo.

  • The ideal would be to convert PHP to boolean false for string "0", No? If the value was null Oh yeah he shouldn’t print anything.

  • True, it would be a bug?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.