Javascript 如何在屏幕上打印?“;console.log不是一个函数";

Javascript 如何在屏幕上打印?“;console.log不是一个函数";,javascript,Javascript,我尝试了很多方法…我做错了什么???我决心学习并真正理解这一点 //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

我尝试了很多方法…我做错了什么???我决心学习并真正理解这一点

//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");

您需要
赋给子字符串返回变量的结果,以获得子字符串结果。使用该变量打印
子字符串结果

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

也许你的错误是每次屏幕上都会出现“珍妮”

这是因为您打印的是常量,而不是变量。请尝试使用此选项进行打印:

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”。这意味着Chrome、安装了Firebug插件的FireFox,或者打开了开发者工具的Internet Explorer。然后当然打开开发者工具。对于Chrome,它是ctrl-shift-I;其他一切都是F12。

字符串不可变,因此您应该将结果分配给一个新变量。

确保您使用的浏览器支持
控制台。log
(Firefox with Firebug,Google Chrome)如果您的浏览器有问题,请尝试在JSFIDLE中运行代码

编辑

我曾尝试在JSFIDLE中运行您的代码,在我的浏览器(Chrome和IE9)中,它们都能完美地工作


根据我读到的评论,你正在使用firefox。如果是这种情况,请确保已安装firebug插件。你可以得到它

谢谢大家的帮助!我真的很感谢你能回答问题,而且知识渊博。我确实弄明白了。显然问题出在子字符串上。我已经把substring这个词全部打出来了。我猜它不喜欢这样,只需要缩写的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打印出您的var值,您应该在console.log的paratenthesis之间包含变量

这在下面的示例中显示

//在第2行,声明一个变量myName并为其命名。 var myName=“珍妮”

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

我发现
console.log不是一个函数
错误,花了令人沮丧的15分钟调试它。原来我有一个名为
console
的局部变量!使用
window.console.log(“…”)
成功了。

可能是您为console.log分配了其他内容,而不是函数console.log=5 console.log()


//显然不再引用函数,请重新启动您的环境。ie.刷新页面

我经常遇到的错误是:TypeError:console.log不是一个函数,这也是我感到困惑的部分原因。如果使用ie:,(如果您使用不同的浏览器/环境,请根据需要进行搜索。)@Tooheyomster好吧,如果在帖子中包含此类信息,这将很容易解决。我怀疑这是因为使用了旧版本的IE。在这种情况下,请确保安装Firebug。这不是问题所在。