zardilior
zardilior

Reputation: 2988

Var status doesn't evaluates into an array on js

If i write the following in the js console

var status = ["POR ENTREGAR","POR ENTREGAR","POR ENTREGAR"];
status;

It evaluates to a string "POR ENTREGAR,POR ENTREGAR,POR ENTREGAR" why is this the result and not the equivalent array? I'm going nuts

If you then write status[0] it returns P; Please before answering try with another array so you can see how the console prints strings and arrays differently.

Upvotes: 0

Views: 97

Answers (3)

KooiInc
KooiInc

Reputation: 123006

status Is a (sort of) reserved word1, so assign the array to another variable name, or assign it within a non global context:

// [status] assigned within the global (window) context
var status   = ["POR ENTREGAR","POR ENTREGAR","POR ENTREGAR"];
var mystatus = ["POR ENTREGAR","POR ENTREGAR","POR ENTREGAR"];
Helpers.log2Screen('`status` is array? ',  (status instanceof Array).yn());
Helpers.log2Screen('`mystatus` is array? ', (mystatus instanceof Array).yn());

statusStatus();

function statusStatus() {
  // [status] assigned within the statusStatus context
  var status   = ["POR ENTREGAR","POR ENTREGAR","POR ENTREGAR"];
  Helpers.log2Screen('`status` (in `statusStatus` context) is array? ',  
                     (status instanceof Array).yn());
}
<script src="http://kooiinc.github.io/JSHelpers/Helpers-min.js"></script>

1 If status is not assigned in the global (i.e. window) context, you're fine, see statusStatus within the snippet. Otherwise, you are overwriting the predefined existing window.status. Actually, window.status changes the text on the status bar of the browser, that's why it is always autoboxed to a string value (note: newer browsers seem to have ditched the status bar (chrome, firefox) or made it readonly (IE), but the thereby useless window property still exists).

Upvotes: 1

Tyler McGinnis
Tyler McGinnis

Reputation: 35286

JavaScript comes with a built in status property on the window object, which is an empty string. Change the variable name to something other than status and you'll be good to go.

Not sure why the downvote. Check it.

Some code which demonstrates the same problem.

    var status = {}
    status ---> "[object Object]"
    typeof status ---> "string"

Upvotes: 2

Alex J
Alex J

Reputation: 1019

If you are looking for a more array format than string format, consider using console.dir(status) or console.table(status)

Upvotes: 0

Related Questions