Programming education has a little tradition: The very first thing you do with any new programming language you learn is to build a little program that prints a "Hello World" to screen. We'll do the same thing.
index.js
. The index
is a common name for the first file your engine should execute, the .js
is the file extension for JavaScript.console.log('Hello World');
Save the file. Then, in your terminal, cd
to the folder the file lives in, and run node index.js
.
In your terminal, you should now be seeing the words Hello world
. Congrats, you wrote your first (tiny) program!
Programs are a collection of instructions we give to the computer. Every programming language contains statements
– individual instructions. In JavaScript, statements are separated by semicolons (;
). If we wanted to print the words Hello World
twice, we could be writing:
console.log('Hello World'); console.log('Hello World');
Since JavaScript uses the semicolon to separate statements, it is okay with us writing on a single line. However, we usually write each statement on its own line for better readability:
console.log('Hello World');
console.log('Hello World');
<aside>
🤔 You will soon discover that there's absolutely nothing in the world of programming that people do not fight about. You may have heard about the tabs
vs spaces
fight. Well, there's another one – debating whether or not one should use semicolons.
When you stick to the "one statement per line" rule, most JavaScript engines will understand what you mean and implicitly assume that you've just finished a statement. However, that's not always true – there are tricky exceptions to this rule.
Since we're cramming a Boot Camp into two days, I suggest that you use semicolons. If you see code that doesn't use them, understand that the developer simply made a choice to omit them.
</aside>
One of the most important pieces of code are statements meant for humans, not computers. The programming world agreed on multiple character sequences to denote that a line contains a comment rather than an instruction and JavaScript supports two of them. Here's an example featuring them both:
// I'm a comment, explaining what's going on
const a = 1 + 1;
/*
I am a multi-line comment. Everything here is a comment.
*/
const b = 1 + 2;
const c = 2 + 2; // Comments can even happen on the same line as statements
// Never before a statement though - this will be ignored: console.log('hi');