INTRODUCTION TO JAVASCRIPT
(In order to get the un-ordered list go to code and put in <ul> </ul> at the front and the end of the part where you want it!)
Console
In JavaScript, the
EX: print
console.log('Woohoo! I love to code! #codecademy');
print log the number
console.log(2011);
console
keyword refers to an object, a collection of data and actions, that we can use in our code. We can write comments in our code that the computer will ignore as our program runs. These comments exist just for human readers.EX: print
'Woohoo! I love to code! #codecademy'
to the console.console.log('Woohoo! I love to code! #codecademy');
print log the number
2011
to the console.console.log(2011);
- "//" single line comment
"/* */" multi=line comment
console.log(/*IGNORED!*/ 5); // Still just prints 5
- seven fundamental data types:
-
Number: Any number including decimals
String: Any grouping of characters on your keyboard, surrounded by single quotes:
' ... '
or double quotes " ... "
Boolean: This data type only has two possible values— either
true
or false
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.
Object: Collections of related data.
Arithmetic Operators
An operator is a character that performs a task in our code. JavaScript has several built-in in arithmetic operators, that allow us to perform mathematical calculations on numbers. These include the following operators and their corresponding symbols:
- Add:
+
- Subtract:
-
- Multiply:
*
- Divide:
/
- Remainder:
%
EX: console.log(0.2708 * 100);
String Concatenation
This process of appending one string to another is called concatenation. Just like with regular math, we can combine, or chain, our operations to get a final result.
EX: console.log('One' + ', ' + 'two' + ', ' + 'three!');
Properties
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.
EX: console.log('Hello'.length);
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.Methods
we use
console.log()
we’re calling the .log()
method on the console
object.Built-in Objects
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:
Math.random() * 50;
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.
Math.floor(Math.random() * 50);
Use the
.ceil()
method to calculate the smallest integer greater than or equal to 43.8
.
console.log(Math.ceil(43.8));
What I learned today...
In this lesson, I learned a lot about how the javaScript language work. At first, I thought Java and JavaScript is the same thing, but after this lesson, I learned that it's totally different, as this language is more direct and easy to learn. However, Java allows us to run it in a virtual machine or browser, javaScript can only be run in browsers.
What i'm doing tomorrow...
Tomorrow, I will be moving onto the next lesson Variables.
This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.!
ReplyDeletedefer parsing of js wordpress