Skip to content

console.log and comments

JavaScript Lesson 1 2:37 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
JavaScript is the language your browser already speaks. JavaScript 是你的浏览器本来就会说的语言。
It runs inside the page you are looking at — no installing, no setting up, it is simply there. 它就在你正看着的这个页面里运行——不用安装,不用配置,它本来就在那里。
And the first thing to learn is how to make it say something back. 要学的第一件事,是怎么让它开口说点什么。
That is the console: a panel every browser has, where your program can print, and where you will spend the next few lessons watching what your code does. 那就是控制台: 每个浏览器都有的一个面板,你的程序可以往里面打印内容, 接下来几课,你都会在那里看着你的代码在做什么。
Here is that line, one piece at a time. 我们把这一行拆开来看。
The word console names the thing you are talking to — the panel itself. console 这个词指的是你在对谁说话——就是那个面板本身。
Then a dot, then log, which is the job you are asking it to do. 接着是一个点,然后是 log,也就是你要它做的事。
Whatever you want shown goes in the brackets, in quotation marks if it is text. 你想显示的内容放在括号里,如果是文字就加引号。
And the line finishes with a semicolon, which marks the end of a statement. 这一行以分号结束,分号表示一条语句到此为止。
JavaScript will often forgive a missing one; write it anyway. 少写一个分号,JavaScript 常常会替你补上;但还是要写。
The quotes decide what happens. 引号决定了会发生什么。
Put five plus three inside quotes and the console shows those five characters exactly as typed, because you asked for text. 把 5 + 3 放进引号里,控制台就照你打的把这几个字符原样显示出来, 因为你要的是文字。
Write it without the quotes and JavaScript sees an actual sum: it is worked out first and the console shows eight. 不加引号写,JavaScript 看到的是一个真正的算式: 它会先算出来,控制台显示 8。
Same characters on your keyboard, two completely different meanings. 键盘上打的是同样的字符,含义却完全不同。
Two slashes start a comment. 两个斜杠开始一段注释。
Everything after them on that line is ignored completely — it is a note for whoever reads the code, and the console never sees it. 那一行里它后面的所有内容都会被完全忽略—— 它是写给读代码的人看的笔记,控制台永远看不到它。
One more detail that costs beginners a lot of time: the name is all lower-case. 还有一个细节会让初学者花掉不少时间:这个名字全是小写。
Capital C or capital L is a different word as far as JavaScript is concerned, and it will tell you it does not know what you mean. 对 JavaScript 来说,大写的 C 或大写的 L 就是另一个词, 它会告诉你它不知道你在说什么。
Now the lesson's second task: print Red, Green and Blue on separate lines. 现在做课程里的第二道题:把 Red、Green、Blue 分行打印出来。
There is no trick here — three colours, one call each, one per line. 这里没有什么花招——三个颜色,每个一次调用,每行一个。
Run it and the console fills from the top in the order you wrote them, because each log finishes its line before the next one starts. 运行它,控制台会按你写的顺序从上往下填满, 因为每个 log 都会在下一个开始之前结束自己那一行。
If your colours come out in the wrong order, look at the order of your lines. 如果颜色出来的顺序不对,那就看看你那几行的顺序。
Four things to take with you. 带走四点。
One: console dot log shows something in the console. 第一:console.log 会在控制台里显示东西。
Two: quotes make text, and no quotes means JavaScript works it out first. 第二:引号表示文字,不加引号表示让 JavaScript 先算出来。
Three: two slashes start a comment. 第三:两个斜杠开始注释。
Four: it is all lower-case — console dot log, every time. 第四:全部小写——每次都是 console.log。
Now do the three tasks; the third one is a sum, so leave the quotes off. 现在去做那三道题;第三题是一个算式,所以别加引号。

Log in or create account

IGCSE, A-Level & AP