How to Instruct a Computer
Let's write some code to make the computer perform simple calculations and display the results!
const a = 1; const b = 2; const c = 3; console.log((a + c) * b);
Here,
console.logis a function that displays values on the screen, showing whatever value is passed within the parentheses.
const is a keyword that defines a constant value which does not change during the execution of the program. In const a = 1, we are assigning the value of 1 to a constant a.
In this context, a keyword is a term with a special meaning in JavaScript that users cannot use as variable names, and = is the operator used to assign values to constants.
The Meaning of =
In programming, the = operator is used not to signify "equals," as learned in math class, but instead to mean "assign the value on the right to the left."
Similarly, we assign the value 2 to constant b, and 3 to constant c.
The Meaning of the Multiplication Operator *
In (a + c) * b, the asterisk (*) represents multiplication.
As a reference, division in programming languages uses a slash (
/).
Thus, the result of multiplying the summed value of a and c by b, which is 4 * 2 = 8, is passed to console.log.
Displaying Output
console.log is a function that outputs the value passed within its parentheses. Without console.log, the result of the operation (a + c) * b would not be displayed on the screen.
Simple, isn't it?
We successfully instructed the computer to perform a simple calculation and output task using JavaScript!
In the next lesson, we'll learn about adding simple comments to JavaScript code.
In JavaScript, does the = operator mean 'equals' as it does in mathematics?
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result