-1

There is something like this found in my javascript homework. Is this valid, or did they forget to put the braces?

  var squares = [], 
    SIZE = 3,
    EMPTY = " ",
    score,
    moves,
    turn = "X";
Hye Shusho
  • 311
  • 2
  • 12
  • 6
    That’s not an object; it’s just a bunch of variables. – Ry- Feb 15 '17 at 01:01
  • 2
    This is called "Multiple variable declaration" http://stackoverflow.com/questions/4166785/how-to-define-multiple-variables-on-a-single-line – bhspencer Feb 15 '17 at 01:03
  • 2
    @syarul a) that is rude. b) I wouldn't be asking in the first place if I wasn't confused c) that is how you learn a language is by asking "silly" questions d) instead of wasting your time being rude, try and be helpful to someone else. – Hye Shusho Feb 15 '17 at 01:18
  • 1
    @HyeShusho Good question, by the way. So bad feedback of the community prominent sign the system cannot fulfill advance requests. Stackoverflow can only cope with really primitive questions. The system spits out any trickier one. – Kos May 10 '17 at 17:48

2 Answers2

3

There are 6 variables being declared in your code. It has nothing to do with an object.

squares is an array, size is a number (3), empty is a string ( ), score and moves are undefined and turn is a string (X)

Google javascript comma operator

EDIT: Declare variables used in scope

var doStuff = function() {
    var i,
        c = 2,
        stuff = "stuff";


};

Rather than:

var doStuff = function() {
     //some code
     for( var i = 0; i <= 10; i++ ) {
     //
     }

     //some code
     var c = 2;

     //some code
     //some code

     var stuff = "stuff";


};

As it lets developers see all the variables that are declared in that scope at a single glance, rather than having to search through the block to see what vars are being declared/used.

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • Of course it's allowed, if it wasn't you'd get a syntax error. Yes, it's quite often done. It's (arguably) good practice to declare all the variables you will use in a given scope at the top of the block (see example in edit). – Adam Jenkins Feb 15 '17 at 01:21
2

They didn't forget. Your teacher just didn't repeat the term 'var' for every variable.

That's the same as:

var squares = [];
var SIZE = 3;
var EMPTY = "&nbsp;";
var score;
var moves;
var turn = "X";
jpfrr
  • 31
  • 4