var X=X||x;

what does the following javascript syntax do: var X=X||x;

and second problem, what does "x=x1=x2=x3;" do?

Comments

  • Issue 1:
    var X=X||x; Declares the global variable X as equal to Uppercase X or lowercase x. Don;t know what the second line of script does.

    Issue 2:
    It looks like they are trying to declare x (lowercase) as equal to x1 x2 and x3, but this statement is syntatically incorrect, if my above assumption is correct.

    Hope that helps ...

    : what does the following javascript syntax do: var X=X||x;
    :
    : and second problem, what does "x=x1=x2=x3;" do?
    :



  • It looks like a C++ expression.
    If you read it from the left to the right it means:
    - make x2 equal to x3
    - make x1 equal to x2
    - make x equal to x1

    But I don't know if that is also valid for JS.
    You should try it with

    var x, x1, x2, x3;
    x3 = 2;
    alert(x);
    alert(x3);
    x = x1 = x2 = x3;
    alert(x);
    alert(x3);

    : and second problem, what does "x=x1=x2=x3;" do?

  • : what does the following javascript syntax do: var X=X||x;
    :
    : and second problem, what does "x=x1=x2=x3;" do?
    :

    Hi there,

    var X = X || x;

    The Logical OR (||) evaluates expression1 (X) for it's truth. If it is false, it returns expression2 (x).

    If this is the case...

    [code]

    var X = 1;
    var x = 0;

    X = X || x;

    alert(X); // Returns 1 since X is true

    X = 0;
    x = 1;

    X = X || x;

    alert(x); // Returns 1 since X is false

    X = 0;
    x = 0;

    X = X || x;

    alert(x); // Returns 0 since X is false

    [/code]

    x=x1=x2=x3;

    This is a Perl/C++ and the like Multiple Assignment Statement.

    Basically it works like this.

    [code]

    var X = 1;
    var Y = X;

    [/code];

    When you assign a value to a variable, it evaluates to the value you assigned. IE. When you assign 1 to X (ie. var X = 1;), it "returns" the value 1. When you assign X to Y (ie. var Y = X;), it "returns" the value of 1 again since X = 1.

    HTH
    Bradley q:)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories