How to Instruct a Computer
Let's write some code to instruct the computer to perform simple calculations and output the result :)
const x = 1;
const y = 2;
const z = 3;
console.log((x + z) * y);
const
is the keyword used to define a constant value that does not change throughout the program's execution.
const x = 1
means assigning the value 1 to the constant x.
Here, a keyword is a word with special meaning that JavaScript has already pre-defined and cannot be used by the programmer for naming variables or functions. The =
operator is used to assign a value to a constant.
The meaning of =
In programming, the =
operator is used to assign a value rather than indicating equality as it does in mathematics.
Similarly, we assign 2 to the constant y and 3 to the constant z.
The meaning of the Multiplication Operator *
In (x + z) * y
, the asterisk (*
) signifies multiplication. By the way, division in programming languages is represented by the slash (/
).
So, you add x and z which results in (x + z)
and then multiply the result by y. The computed value, i.e., 4 * 2 = 8, is passed to console.log
.
Outputting
console.log
is a function that prints the value passed within its parentheses (()
). Without console.log
, the result of (x + z) * y
would not be displayed on the screen.
Pretty simple, right?
This way, we have instructed the computer to perform simple calculations and output tasks using JavaScript!
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.