In this lesson, you will learn introductory coding concepts including data types and built-in objects—essential knowledge for all aspiring developers. Make sure to take notes and pace yourself. This foundation will set you up for understanding the more complex concepts you’ll encounter later.
The console is a panel that displays important messages, like errors, for developers. Much of the work the computer does with our code is invisible to us by default. If we want to see things appear on our screen, we can print, or log, to our console directly.
One action, or method, that is built into the console object is the .log() method. When we write console.log() what we put inside the parentheses will get printed, or logged, to the console.
It’s going to be very useful for us to print values to the console, so we can see the work that we’re doing.
You’ll see later on that we can use console.log() to print different kinds of data.
Programming is often highly collaborative. In addition, our own code can quickly become difficult to understand when we return to it— sometimes only an hour later! For these reasons, it’s often useful to leave notes in our code for other developers or ourselves.
Comments can explain what the code is doing, leave instructions for developers using the code, or add any other useful annotations.
- A single line comment will comment out a single line and is denoted with two forward slashes // preceding it.
|// Prints 5 to the console
- You can also use a single line comment to comment after a line of code:
|console.log(5); // Prints 5|
- A multi-line comment will comment out multiple lines and is denoted with /* to begin the comment, and */ to end the comment.
This is all commented
None of this is going to run!
- You can also use this syntax to comment something out in the middle of a line of code:
|console.log(/*IGNORED!*/ 5); // Still just prints 5|
- Number: Any number, including numbers with decimals: 4, 8, 1516, 23.42.
- String: Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single quotes: ‘ … ‘ or double quotes ” … “. Though we prefer single quotes. Some people like to think of string as a fancy word for text.
- Boolean: This data type only has two possible values— either true or false(without quotes). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.
- Null: This data type represents the intentional absence of a value, and is represented by the keyword null(without quotes).
- Undefined: This data type is denoted by the keyword undefined (without quotes). It also represents the absence of a value though it has a different use than null.
- Symbol: A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now.
- Object: Collections of related data.
But before we do that, let’s get comfortable with strings and numbers!
In the example above, we first printed a string. Our string isn’t just a single word; it includes both capital and lowercase letters, spaces, and punctuation.
Next, we printed the number 40, notice we did not use quotes.
Basic arithmetic often comes in handy when programming.
- Add: +
- Subtract: –
- Multiply: *
- Divide: /
- Remainder: %
The first four work how you might guess:
|console.log(3 + 4); // Prints 7
console.log(5 – 1); // Prints 4
console.log(4 * 2); // Prints 8
console.log(9 / 3); // Prints 3
Note that when we console.log() the computer will evaluate the expression inside the parentheses and print that result to the console. If we wanted to print the characters 3 + 4, we would wrap them in quotes and print them as a string.
The remainder operator, sometimes called modulo, returns the number that remains after the right-hand number divides into the left-hand number as many times as it evenly can: 11 % 3 equals 2 because 3 fits into 11 three times, leaving 2 as the remainder.
Operators aren’t just for numbers! When a +operator is used on two strings, it appends the right string to the left string:
|console.log(‘hi’ + ‘ya’); // Prints ‘hiya’
console.log(‘wo’ + ‘ah’); // Prints ‘woah’
console.log(‘I love to ‘ + ‘code.’)
// Prints ‘I love to code.’
This process of appending one string to another is called concatenation. Notice in the third example we had to make sure to include a space at the end of the first string. The computer will join the strings exactly, so we needed to make sure to include the space we wanted between the two strings.
|console.log(‘front ‘ + ‘space’);
// Prints ‘front space’
console.log(‘back’ + ‘ space’);
// Prints ‘back space’
console.log(‘no’ + ‘space’);
// Prints ‘nospace’
console.log(‘middle’ + ‘ ‘ + ‘space’);
// Prints ‘middle space’
Just like with regular math, we can combine, or chain, our operations to get a final result:
|console.log(‘One’ + ‘, ‘ + ‘two’ + ‘, ‘ + ‘three!’);
// Prints ‘One, two, three!’
|console.log(‘Hello’.length); // Prints 5|
The . is another operator! We call it the dot operator.
In the example above, the value saved to the length property is retrieved from the instance of the string, ‘Hello’. The program prints 5 to the console, because Hello has five characters in it.
We call, or use, these methods by appending an instance with a period (the dot operator), the name of the method, and opening and closing parentheses: e.g. ‘example string’.methodName().
Does that syntax look a little familiar? When we use console.log() we’re calling the .log() method on the console object. Let’s see console.log() and some real string methods in action!
|console.log(‘hello’.toUpperCase()); // Prints ‘HELLO’
console.log(‘Hey’.startsWith(‘H’)); // Prints true
Let’s look at each of the lines above:
- On the first line, the .toUpperCase()method is called on the string instance ‘hello’. The result is logged to the console. This method returns a string in all capital letters: ‘HELLO’.
- On the second line, the .startsWith()method is called on the string instance ‘Hey’. This method also accepts the character ‘H’ as an input, or argument, between the parentheses. Since the string ‘Hey’ does start with the letter ‘H’, the method returns the boolean true.
The great thing about objects is that they have methods! Let’s call the .random()method from the built-in Math object:
|console.log(Math.random()); // Prints a random number between 0 and 1|
In the example above, we called the .random() method by appending the object name with the dot operator, the name of the method, and opening and closing parentheses. This method returns a random number between 0 and 1.
To generate a random number between 0 and 50, we could multiply this result by 50, like so:
|Math.random() * 50;|
The example above will likely evaluate to a decimal. To ensure the answer is a whole number, we can take advantage of another useful Math method called Math.floor().
Math.floor() takes a decimal number, and rounds down to the nearest whole number. You can use Math.floor() to round down a random number like this:
|Math.floor(Math.random() * 50);|
In this case:
- Math.random generates a random number between 0 and 1.
- We then multiply that number by 50, so now we have a number between 0 and 50.
- Then, Math.floor() rounds the number down to the nearest whole number.
To see all of the properties and methods of the Math object, take a look at the documentation here.
Let’s take one more glance at the concepts we just learned:
- Data is printed, or logged to the console, a panel that displays messages with console.log().
- You can write single-line comments with // and multi-line comments between /*and */.
- Numbers are any number without quotes: 23.8879
- Strings are characters wrapped in single or double quotes: ‘Sample String’
- The built-in arithmetic operators include +, –, *, /, and %.
- Objects, including instances of data types, can have properties, stored information. The properties are denoted with a . after the name of the object, for example: ‘Hello’.length.
- Objects, including instances of data types, can have methods which perform actions. Methods are called by appending the object or instance with a period, the method name, and parentheses. For example: ‘hello’.toUpperCase().
- We can access properties and methods by using the ., dot operator.