簡體   English   中英

如何打印到屏幕 - “console.log不是一個功能”

[英]How to print to the screen - “console.log is not a function”

我嘗試過很多方法......我做錯了什么? 我決心學習並真正理解這一點。

//On line 2, declare a variable myName and give it your name.
var myName = "Jeanne";
//On line 4, use console.log to print out the myName variable.
console.log ("Jeanne");
//On line 7, change the value of myName to be just the first 2 letters of your name.
myName.substring(0, 2);
//On line 9, use console.log to print out the myName variable;
console.log("Jeanne");

如果您的瀏覽器出現問題,請確保您使用的瀏覽器支持console.log (Firefox with Firebug,Google Chrome),嘗試在jsfiddle中運行您的代碼

編輯

我試圖在jsfiddle中運行你的代碼,在我的瀏覽器(Chrome和IE9)中,它們都運行得很好。

根據我讀過的評論你使用的是Firefox。 如果是這樣的話,請確保安裝了firebug插件。 你可以在這里得到它

您需要assign子字符串的結果assign回變量以獲取子字符串結果。 使用該varible打印substring result

現場演示

myName = myName.substring(0, 2);
console.log(myName );

可能你的錯誤是每次都在屏幕上顯示“Jeanne”。

這是因為你打印的是常數,而不是你的變量。 嘗試使用它進行打印:

myName = myName.substring(0, 2);
console.log (myName);

並且如其他答案中所述 - 確保您使用的是支持console.log的瀏覽器

你正在做這一切正確只記錄已處理的變量而不是原始名稱,完成!

//On line 2, declare a variable myName and give it your name.
var myName = "Jeanne";
//On line 4, use console.log to print out the myName variable.
console.log (myName );
//On line 7, change the value of myName to be just the first 2 letters of your name.
myName=myName.substring(0, 2);
//On line 9, use console.log to print out the myName variable;
console.log(myName );

除此之外,請確保您使用的瀏覽器支持“console.log”。 這意味着安裝了Firebug插件的Chrome或FireFox,或打開開發人員工具的 Internet Explorer。 然后當然打開開發人員工具。 對於Chrome,它是ctrl-shift-I; 其他一切都是F12。

為了讓console.log打印出你的var值,你應該在console.log的 paenthesis之間包含變量

這在下面的例子中顯示;

//在第2行,聲明一個變量myName並給它命名。 var myName =“Jeanne”;

//在第4行,使用console.log打印出myName變量。 console.log(myName);

不可變的字符串,因此您應該將結果分配給新變量。

謝謝你們每一個人的幫助! 我非常感謝你能夠回答問題並且知識淵博。 我確實搞清楚了。 顯然問題在於子字符串。 我已經完整地輸入了字符串子字符串。 我想它不喜歡那樣,只需要縮寫的substr。

//On line 2, declare a variable myName and give it your name.
var myName = "Jeanne";
//On line 4, use console.log to print out the myName variable.
console.log (myName);
//On line 7, change the value of myName to be just the first 2 
//letters of your name.
myName = myName.substr(0,2);
//On line 9, use console.log to print out the myName variable;
console.log (myName);

我有console.log is not a function錯誤,花了一個令人沮喪的15分鍾調試它。 事實證明我有一個名為console的局部變量! 使用window.console.log("...")就可以了。

大概是你為console.log分配了一些東西,這不是函數console.log = 5 console.log()

//顯然不再引用函數重啟環境了。 即。 刷新頁面

暫無
暫無

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

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