Hey Programmer! Welcome to the JavaScript module. In the next few sections, you'll be learning the fundamentals of the JavaScript programming language. If it's your first time programming, don't worry; you'll be executing your first lines of code in no time!
The first command you'll learn in JavaScript is console.log. This command is used to print something onto the screen. As you write your first lines of code, you'll be using console.log frequently as a way to visually see the output of your programs.
For example, here is a simple two-line program:
console.log("hello world");
console.log("how are you?");
Executing the program above--more on how to do that in a subsequent reading!--would print the following to the screen:
hello world
how are you?
Nothing too ground breaking here, but pay close attention to the exact way we wrote the program. In particular, notice how the periods, parentheses, and quotation marks are laid out. Lines of code are also terminated with semicolons (;), sort of like a period (.) to denote the end of an English sentence.
The quotation marks (") note the start and end of a JavaScript string. A string represents text data. console.log("hello world") prints the string hello world.
Depending on how you structure your code, sometimes you'll be able to omit semicolons at the end of lines. For now, you'll want to include them as shown here.
NOTE: In real world applications, console.log should be used only for debugging purposes. Debugging means locating and solving errors in your code. console.log is mostly used as a way to locate the error(s) in your code.
Syntax is the exact arrangement of the symbols, characters, and keywords in code. These details matter: your computer will only be able to "understand" proper JavaScript syntax. A programming language is similar to a spoken language. A spoken language like English has grammar rules that you should follow in order to be understood by fellow speakers. In the same way, a programming language like JavaScript has syntax rules that you need to follow in order to be understood by a computer.
As you write your first lines of code in this new language, you will probably make many syntax errors. Don't get frustrated! This is normal, all new programmers go through this phase. Every time you recognize an error in your code, you have an opportunity to reinforce your understanding of correct syntax. Adopt a growth mindset and learn from your mistakes.
Additionally, one of the best things about programming is that you can get immediate feedback from your creations. There is no penalty for making a mistake when programming. Write some code, run the code, read the errors, fix the errors, rinse and repeat!
Occasionally you'll want to leave comments or notes in your code. Commented lines will be ignored by your computer. This means that you can use comments to write plain English or temporarily avoid execution of some JavaScript lines.
In JavaScript, you can denote single-line comments as double forward slashes (//). You can add them in as their own line or tack them on to the end of a line of code.
// This is a comment
console.log('Hello World!'); // This is also a comment
Comments are also useful for disregarding lines of code that you don't want to be executed by the computer at runtime.
// Not sure if I'm going to be using this code or not:
// console.log('Hello World!);
// This code will run:
console.log('Goodbye Moon.');
The program above will print only:
Goodbye Moon.
In JavaScript, you can denote multi-line comments by starting the comment with a forward slash and an asterisk (/*) and ending the comment with an asterisk and a forward slash (*/).
/*
This is a multi-line comment!
Great for documentation.
When this code runs, it will print 'Hello World!' to the console.
*/
console.log('Hello World!');
Comments are sometimes used as notes or documentation alongside lines of code. They are useful when annotating pieces of code to offer an explanation of how the code works. You'll want to strive to write straightforward code that is self-explanatory when possible, but you can also use comments to add additional clarity when necessary. The real art of programming is to write code so elegantly that it is easy to follow.
"Simplicity is prerequisite for reliability." — Edsger W. Dijkstra
console.log can be used to print to the screen.console.log is used for locating errors in real-world applications.// will turn anything from the // to the end of the line into a comment./* at the start and */ at the end will turn that text into a multi-line comment.