Reputation: 6352
I am trying to print text as binary value on console, but the result is "[object Window]".
console.log(toString(number, 2));
Upvotes: 0
Views: 289
Reputation: 6338
toString is a method, not a function. Since calling functions in javascript calls them from the window object, you get [object Window]
console.log(number.toString(2));
will convert a number into binary.
ex:
var num = 15;
console.log(num.toString(2));
> num = 1111;
Upvotes: 1
Reputation: 97718
Like most things in JS, toString
is a method on the particular object, not a global function. See this MDN page, with examples.
So you want:
console.log(number.toString(2));
What is happening in your code is that it is liooking for some object to call toString
on, and finding the "root object", which is window
. So your code translates to this:
console.log(window.toString(number, 2));
Since window.toString
doesn't take any arguments, they are ignored, meaning it's just like running this:
console.log(window.toString());
Upvotes: 2