Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

Does Javascript converts numbers and other data types to strings to print it in console?

So I was reading a book on JS , and it says if you write

var x = 20;
console.log(x);

Then Javascript will first convert x to string implicitly then print it. Can't Javascript just print numbers on console ?

question from:https://stackoverflow.com/questions/65896280/does-javascript-converts-numbers-and-other-data-types-to-strings-to-print-it-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The book isn't quite right. Unless you deliberately concatenate with a string, the console will generally log the argument as-is. Numbers will be logged as numbers, strings will be logged as strings, objects will be logged as (expandable, navigable) objects, and so on.

console.log(20);
console.log('20');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...