Logic Operator ||= in AS3

Well, few people knows about the logic operator “||=”. I’ll try to explain a little more about it.

In the adobe documentation, this operator is described like: “Assigns expression1 the value of expression1 || expression2”. Just to remember, the sintaxe to logic operator is “expression1 operator expression2“.

I’ll show some examples of how it works.

Exemple1:

1
2
3
var array:Array; // Variable arr is null.
array ||= ["Bruno Sales"]; // In case array is null, assign the value ["Bruno Sales"]
trace(array.toString()); // Result: New value assigned, trace prints "Bruno Sales"

Exemple2:

1
2
3
var array:Array = ["DClick"]; // Variable arr has the value ["DClick"].
array ||= ["Bruno Sales"]; // In case array is null, assign the value ["Bruno Sales"]
trace(array.toString()); // Result: No value assigned, trace prints "DClick"

Soon, looking the difference of this 2 exemples, we conclued that the logic operator “||=” makes the same thing of the code below, but in a better way.

1
2
3
4
5
var array:Array = ["DClick"];
if (array == null)
{
array = ["Bruno Sales"];
}

I hope you all liked it. 🙂


One comment

  1. тут on 20.ago.21 at 5:08 am

    Мне нравится потратить полчаса за чашечкой чая,
    читая сообщения каждый день.

Leave a Comment