- Getting Started
- Variables and Types
- Arrays
- Manipulating Arrays
- Operators
- Conditions
- Loops
- Objects
- Functions
- Callbacks
-
Object-Oriented JavaScript - Function Context
- Inheritance
Based on Javascript Tutorial
Operators
Every variable in JavaScript is casted automatically so any operator between two variables will always give some kind of result.
The addition operator
The +
(addition) operator is used both addition and concatenation of strings.
For example, adding two variables is easy:
var a = 1;
var b = 2;
var c = a + b; // c is now equal to 3
The addition operator is used for concatenating strings to strings, strings to numbers, and numbers to strings:
var name = "John";
print("Hello " + name + "!");
print("The meaning of life is " + 42);
print(42 + " is the meaning of life");
Mathematical operators
To subtract, multiply and divide two numbers, use the minus (-
), asterisk (*
) and slash (/
) signs.
print(3 - 5); // outputs -2
print(3 * 5); // outputs 15
print(3 / 5); // outputs 0.6
Advanced mathematical operators
JavaScript supports the modulus operator (%
) which calculates the remainder of a division operation.
print(5 % 3); // outputs 2
Instead of typing something like myNumber=myNumber*2, you can use myNumber*=2. There are the following shortcuts like that: *= -= += /=
JavaScript also has a Math
module which contains more advanced functions:
Math.abs
calculates the absolute value of a numberMath.exp
calculates e to the power of a numberMath.pow(x,y)
calculates the result of x to the power of yMath.floor
removes the fraction part from a numberMath.random()
will give a random numberx
and 0<=x<1
And many more mathematical functions.
Challenge
In this exercise, you do the following:
- Connect the
firstName
andlastName
to construct the variablefullName
, but with a space (" "
) in between the first and last name. - Multiply the variable
myNumber
by 2 and put the result inmeaningOfLife
.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.