Untitled
Untitled
Untitled
console.log()
The console.log() method is used to log or print messages to the console. It can
also be used to print objects and other info.
Comments:
You can use comments to explain what the code is doing, leave instructions for
developers using the code, or add any other useful annotations.
2. Multi-line comment: It can comment out multiple lines and you use /* to begin
the comment and */ to close it.
/*
console.log(555556);
console.log(648);
console.log(44);
*/
Multi-line comments are often best suited to prevent a block of code from running.
If it works, you can delete the code you commented out. If it doesn’t, you can
delete the new code, and un-comment the commented out code. Now you are back to
where you started, and didn’t have to re-type your original code. It is also useful
for tracking down errors. You may want to comment out a block of code to see if the
rest of the code is working correctly.
Data types:
Data types are the classifications we give to the different kinds of data that we
use in programming. In JavaScript, there are seven fundamental data types:
3. 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.
4. Null: This data type represents the intentional absence of a value, and is
represented by the keyword -null- (without quotes).
6. Symbol: A newer feature to the language, symbols are unique identifiers, useful
in more complex coding. No need to worry about these for now.
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, and you’ll learn much
more about them as you progress through JavaScript.
Arithmetic Operators:
1. Add: +
2. Subtract: -
3. Multiply: *
4. Divide: /
5. Remainder: %
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.
String Concatenation:
Operators aren’t just for numbers! When a + operator is used on two strings, it
appends the right string to the left string:
Properties:
When you introduce a new piece of data into a JavaScript program, the browser
saves it as an instance of the data type. Every string instance has a property
called -length- that stores the number of characters in that string. You can
retrieve property information by appending the string with a period and the
property name:
console.log('Hello'.length); // Prints 5
Methods:
Remember that methods are actions we can perform. JavaScript provides a number of
string methods.
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!
Built-in Objects:
In addition to console, there are other objects built into JavaScript. Down the
line, you’ll build your own objects, but for now these “built-in” objects are full
of useful functionality.
For example, if you wanted to perform more complex mathematical operations than
arithmetic, JavaScript has the built-in Math object.
The great thing about objects is that they have methods! Let’s call the
-.random()- method from the built-in Math object:
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);
If you wanted to see the number printed to the terminal, you would still need to
use a console.log() statement: