簡體   English   中英

Console.log vs打印輸出變量

[英]Console.log vs printing out variable

當我使用console.log在控制台窗口中為一個不在引號中的字符串打印一個值時,但是當我在控制台中僅輸出變量時,它就會打印引號。 有什么特殊原因嗎?

var test = “hello”; 
test; 
Output : “hello”

Console.log(test); 
Output: hello

好吧,看到這樣。

 var test = "hello"; test; // This is object in self and what is it, // it is a string in literal // Output comes only from debugger , when you input direct. // test; this line have no output to the console // Output : "hello" NO //console.log(test); // console.log already print string (in native/string is output) but also print objects. // Output: hello YES console.log( test + " this is the test") // See output it is a very clear // hello this is the test 

默認行為是在控制台中將字符串與引號一起表示。

a = 'hi';
a
// returns "hi"

控制台 api是不同的,並且是一個例外。

console.log(object [,object,...])在控制台中顯示一條消息。 將一個或多個對象傳遞給此方法。 對每個對象求值並將其連接成一個以空格分隔的字符串。

因此,它返回一個以空格分隔的連接字符串。 這意味着它將始終是字符串。 由於它始終是字符串,因此我們可以刪除引號。 我想控制台開發人員可以這樣說,使console.log()始終返回相同的類型(字符串)。 添加引號可能意味着它可能返回其他內容,因此對於控制台來說,這似乎是UX的事情。

 var a = "hello"; // To check the return type of variable a which is string console.log(typeof(a)); // To check the return type of console.log() which is undefined console.log(typeof(console.log(a))); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM