Lesson 3: Arithmetic
An operator is a special type of code typically used to modify a piece of data in some way. Some common operators include mathematical operators +
, -
, *
, /
.
let my_number: number = 20;
let my_other_number: number = my_number + 5;
console.log(my_other_number)
As we can see, writing math formulas in code looks very similar to how it would on a piece of paper, with the important difference that the =
operator is not used to merely state that two expressions are the same, but to assign the right-hand expression into a variable on the left.
It is not possible to use the =
operator for anything but a variable on the left-hand side.
Assignment #3.1: Make the above program instead print out the value “100” by changing the operator. (Assignment) (Solution)
String Operations
The +
symbol can also be used on string objects, where it instead acts as a “join” operator to concatenate two string values together into a single string.
let my_first_string: string = "Hello ";
let my_second_string: string = "World!";
let my_third_string: string = my_first_string + my_second_string;
console.log(my_third_string)
Assignment #3.2: Try using the +
operator between a string and a number. What happens and why?