You use console.log() to print messages on the console or log messages. It is also used to print objects.
console.log('Hello World!'); This code snippet prints the message: Hello World!
let single = 'This is an example!'; let double = "Here is an example!";
let amount = 10; let price = 9.99;
let anExample= true;
Null can be described as a data type where a variable is intentionally made to be absent of any value. Thus, the used data type is ‘null’.
let x = null;
You can assign these data type values by using ‘=.’
Comments, though not strictly part of the code, help break down your programming code into easily decipherable text that helps you or others who will be seeing your code to understand what a particular snippet stands for or what is going behind the scenes. In a sense, they are notes for your program so that you can remember why a certain code is in the way it is.
However, you can not simply type comments along with a code statement just as you would do while typing some text. Instead, you need to properly assign comments so that they are not executed when you run your program. The two types of comments are:
Single line comments: These are marked as ‘comment’ with double slashes ( // ) and should only be used when you want to write a small, precise comment.
Long comments: If your comment is of multiple lines and you do not want to execute it, you need to wrap it with /* and */ to mark it as a comment.
Syntax: To call a library, first write the library name, follow it with a period ( . ) and then with the method, and finally the parenthesis.
Here, Math is the library with methods like ‘random’ in it.
The Math.random() function, when called, executes to print a random floating-point from 0 to 0.9.
console.log(Math.random()); // Prints: 0 - 0.9
The Match.floor() function is used to print the largest integer, which is less than or equal to the number provided.
console.log(Math.floor(7.95)); // Prints: 7
Variables can be understood as non-fixed values, which can be assigned values at the time of carrying operations or executing code in case of var or as in stand-in values for other variables.
These are of three types:
var – The var variable can be reassigned and is only accessible from within a function.
const – These variables can not be reassigned and are only accessible when they appear in the code.
let – The variable can be reassigned values but can only be declared once.
Arrays in any programming language are used as a way to sort out variables and properties into groups or categories. Hence, one specific group can have many variables which you can use for further programming.
Example: An array named ‘planet’ can have many variables like ‘earth,’ ‘Jupiter, ‘mars,’ ‘mercury,’ and more.
var planet = ["Earth", "Mars", "Jupiter", "Mercury"];