This Article does the following:
The console is a panel that displays important messages to the debugging console of our browsers.
One action, or method, that is built into the
console object is the
.log() method. When we write
console.log() the parameter passed inside the parentheses will get printed, or logged, to the console.
This example logs
console.log() can be used to print different types of data.
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
//at the beginning or after a line of code.
// Prints Jamiu to the console console.log("Jamiu"); console.log("Jamiu"); // Prints Jamiu
- 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 console.log(Jamiu); None of this is going to run! console.log(Adeleye); */
This syntax can also be used to comment something out in the middle of a line of code:
console.log(/*IGNORED!*/ Jamiu); // Still just prints Jamiu
Number: Any number, including numbers with decimals:
String: Any grouping of characters on our keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single quotes:
' ... 'or double quotes
" ... ".
Boolean: This data type only has two possible values— either
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
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
Symbol: A newer feature to the language, symbols are unique identifiers, useful in more complex coding.
Object: Collections of related data.
The first 6 of those types are considered primitive data types. They are the most basic data types in the language. Objects are more complex.
console.log(3 + 4); // Prints 7 console.log(5 - 1); // Prints 4 console.log(4 * 2); // Prints 8 console.log(9 / 3); // Prints 3 console.log(11 % 3); // Prints 2
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
3 fits into
11 three times, leaving
2 as the remainder.
The process of appending one string to another is called concatenation.
console.log('hi' + 'ya'); // Prints 'hiya' console.log('wo' + 'ah'); // Prints 'woah' console.log('I love to ' + 'code.') // Prints 'I love to code.'
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!'
Properties are used to check pieces of information on a data type.
Every string instance has a property called
length that stores the number of characters in that string. we can retrieve property information by appending the string with a period and the property name:
console.log('Teaching the world how to code'.length); // prints 30
. 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,
'Teaching the world how to code'. The program prints 30 to the console because
'Teaching the world how to code' has thirty characters in it.
these methods are called, or used by appending an instance with a period (the dot operator), the name of the method, and opening and closing parentheses: e.g.
When we use
console.log() we’re calling the
.log() method on the
Some methods are explained below:
.toUpperCase()method: This method returns a string in all capital letters.
console.log('hello'.toUpperCase()); // Prints 'HELLO'
.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:
This method takes in an argument as checks if the string to which it is appended starts with that argument. If it starts with the argument then it prints
true and if it does not start with the argument it prints
console.log('Hey'.startsWith('H')); // Prints true
.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
.trim()method is used to remove white spaces at the beginning and end of a string.
console.log(' Remove whitespace '.trim());// prints Remove whitespace
For example, the built-in
.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 below:
Math.random() * 50; //expected output: 17.793075626901476
The example above will likely evaluate to a decimal. To ensure an answer is a whole number, we can take advantage of another useful Math method called
Math.floor() takes a decimal number, and rounds down to the nearest whole number. we can use
Math.floor() to round down a random number like below:
Math.floor(Math.random() * 50); //expected output: 11
Math.ceil() takes a decimal number, and rounds up to the nearest whole number. we can use
Math.ceil() to round up a random number like this:
Math.ceil(Math.random() * 50); // Expected output: 13