Lesson 1
Diving into JavaScript: Printing, Commenting, Data Types, and Expressions
Overview

Greetings! Today, we're about to start an exciting journey into the world of JavaScript. We'll focus on JavaScript fundamentals - printing, comments, data types, and expressions. Imagine you can make a website play a game with its visitors or react to their actions like a fellow human. That's what JavaScript does! Ready to dive in? Let's start.

Dive into JavaScript

JavaScript brings static web pages to life by making them interactive! Every language has syntax — rules about how sentences (or in our case, statements) should be written. JavaScript is no exception. Let's have a quick look:

JavaScript
1// Outputting text to the console 2console.log("Hello, World!");

In the above example, we're giving a shoutout to the world: "Hello, World!". Remember to always end each statement with a semi-colon (;).

A great start, isn't it? Let's keep going.

Commenting in JavaScript

Coding isn’t just for computers. You also leave notes for fellow humans — future developers or even your future self — known as 'comments'. A comment can either be a one-liner that uses //, or a note stretching across multiple lines within /* and */. These comments explain your code’s purpose, design, and functioning, making reading code much easier.

JavaScript
1// Single line comment 2 3/* 4Multi-line 5comment 6 7console.log(600); 8The above line won't output anything, since it's within a comment 9*/

An essential thing to note is that, unlike JavaScript statements, comments do not need to end with a semi-colon (;). This is because they're not executable code and are ignored by the JavaScript engine.

Understanding Data Types in JavaScript

JavaScript processes three primary data types: Numbers, Strings, and Booleans. Try entering the following commands and observe the results:

JavaScript
1console.log(42); // prints 42 2console.log("Learning is fun!"); // prints "Learning is fun!" 3console.log(true); // prints true
Making JavaScript Expressive

Let's move on to expressions, which are pieces of code that produce a value. It can be numbers, strings, or boolean values. For instance, 2 + 2 yields 4, and "Hello, " + "World!" results in "Hello, World!". Experiment with basic arithmetic operations, like addition (+), subtraction (-), multiplication (*), division (/), and modulo (%, the remainder from division) in the CodeSignal IDE.

Take a look at this combination of comments, print commands, and expressions:

JavaScript
1console.log(2 + 2); // Outputs: 4 2console.log("2 + 2"); // Outputs: 2 + 2 3console.log("Hello, " + "World!"); // Outputs: Hello, World! 4console.log("2" + "2"); // Outputs: 22, because + operation applied on strings concatenates them 5console.log(2 + 2 > 5); // Outputs: false, because 2 + 2 is not greater than 5.
Comparison Operators

Comparison operators include < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to). They are used to compare two values and return boolean true or false.

JavaScript
1console.log(5 < 6); // true 2console.log(5 > 6); // false 3console.log(5 <= 5); // true 4console.log(5 >= 10); // false
Lesson Summary

Fantastic! You've taken some confident strides into the JavaScript world. We've touched upon JavaScript printing, comments, data types, and expressions. Now, you're all set to roll up your sleeves and put your newfound knowledge to practice! So, let's move onto the practice tasks and flex those JavaScript muscles!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.