Welcome to our first lesson of the JavaScript introductory course! Today, we will uncover the simplicity and power of JavaScript, a universally esteemed programming language renowned for its expressiveness and readability. We'll learn and practice in the CodeSignal environment, where JavaScript and all its components come pre-installed. By the end of this lesson, you'll grasp the foundational aspects of JavaScript and be able to execute your very first JavaScript code. Intriguing, isn't it?
Firstly, we'll focus on the essence of JavaScript and its syntax. Every language, whether it's English, Spanish, or JavaScript, operates under a unique syntax. JavaScript, being a language essential to the web, brings interactivity to websites. Its syntax sets the guidelines akin to the rules of English grammar.
Take, for instance, the following JavaScript syntax showcasing code:
JavaScript1console.log("Hello, JavaScript Universe!");
This simple line of code, like a basic English sentence, sends out your first JavaScript message: "Hello, JavaScript Universe!"
.
In JavaScript, the semicolon (;
) acts as a statement terminator, similar to a period in written language. It informs the JavaScript engine that the current statement has concluded, allowing you to start another statement. Here’s a simple analogy for it: just as periods mark the end of sentences in English, semicolons often mark the end of code statements in JavaScript.
Here’s the previous example with an explicit semicolon:
JavaScript1console.log("Hello, JavaScript Universe!");
While JavaScript interpreters can often infer where a statement ends even without a semicolon, known as Automatic Semicolon Insertion (ASI), it's considered good practice to include them for clarity and to prevent any unforeseen issues, especially in complex scenarios.
Remember to end your statements with a semicolon to avoid potential pitfalls and ensure better compatibility across different JavaScript environments.
Coding is a form of art, and akin to every artist, coders leave reflections and significant explanations in their code in the form of comments. Comments are annotations or explanations providing additional insights about the code. They make your code more informative and demonstrative to others (or even to your future self). Comments do not affect your code execution or its outcome in any way but are helpful to better understand what's happening in the code.
In JavaScript, single-line comments are marked with //
and multi-line comments with /* */
, and they're for the benefit of the developer.
Here's a look at a single-line comment:
JavaScript1// This is a single-line comment
Let's explore a multi-line comment:
JavaScript1/* 2This is a multi-line comment that 3extends 4across multiple lines 5*/
These comments provide key explanations and context for developers, but the program ignores them when the code runs.
The console.log()
function assists us in displaying data on the console. Whether it is text, numbers, the result of an operation, or even a complex structure, if it can be represented as text, JavaScript can print it! Here are some examples:
JavaScript1console.log("Hello, JavaScript Space Station!"); // Prints "Hello, JavaScript Space Station!" 2console.log(5); // Prints "5" 3console.log(3 * 7); // Prints "21"
We've successfully navigated through the basics of JavaScript — syntax, comments, indentation, and printing. Now, prepare for exciting practice exercises that will help you master the knowledge you've just acquired. Hold on tight as we venture deeper into the JavaScript cosmos!