An expression evaluates into (results) in a single value. Broadly speaking there are two types of expressions.
// var or let
var color = "beige";
// var or let
var area = 3 * 2;
Expressions rely on things called operators, they allow programmers to create a single value from one or more values.
Operation types are:
var color = "beige";
var area = 3 * 2;
var greeting = "Hi " + "Molly";
var buy = 3 > 5;
var buy = 5 > 3 && 2 < 4;
point 4 and 5 covered in chapter 4
JavaScript contains the following mathematical operators, which you can use with numbers.
Name | Operation | Purpose & notes | Example | result |
---|---|---|---|---|
ADDITION | + | Adds one value to another | 10 + 5 | 15 |
SUBTRACTION | - | Subtracts one value from another | 10 - 5 | 5 |
DIVISION | / | Divides two values | 10 / 2 | 2 |
MULTIPLICATION | * | Multiplies t wo values using an asterisk not x |
10 * 5 | 50 |
INCREMENT | ++ | Adds one to the current number | i = 10; i++; | 11 |
DECREMENT | ā | Subtracts one from the current number | i = 10; iā; | 9 |
MODULUS | % | Divides two values and returns the remainder | 10 % 3 | 1 |
We have to take care of those rules with arithmetic operations:
total 2 + 4 * 10; // Result ??
total = (2 + 4) * 10; // result ??
// Ahmed write them c02/js/arithmetic-operator Program
There is just one string operator: the + symbol. It is used to join the strings on either side of it.
ES5
var firstName = "Ivy ";
var lastName = " Stone";
var fullName = firstName + lastName; // Ivy Stone
ES6
let firstName = "Ivy ";
let lastName = " Stone";
let fullName = `${firstName} ${lastName}`;
When you place quotes around a number, it is a string (not a numeric data type), and you cannot perform addition operations on strings
var costl = "7";
var cost2 = "9 ";
var total = costl + cost2; // '79' as string not a number
If you try to add a numeric data type to a string, then the number becomes part of the string.
var number = 12;
var street = "Ivy Road";
var add = number + street; // '12Ivy Road'
If you try to use any of the other arithmetic operators on a string, then the value that results is usually a value called NaN
means not a number.
var score = "seven ";
var score2 = " nine";
var total = score * score2; // NaN
// Ahmed write them c02/js/string-operator Program
:information_source: Read more about Camel case :information_source: Read more about Snake case