Your first JavaScript program
JavaScript evaluates expressions and can report their values in a console.
How it works
A script reads each statement in order until it encounters a control-flow instruction. Console.log receives a value and displays it in developer tools. Putting a calculation inside it tells JavaScript to calculate that expression first.
The sample logs Hello and then 6. These lines appear in the developer console. To place a message on the page itself, a script would update a page element.
Walk through an example
A browser developer console can evaluate console.log("Hello"). console.log(2 * 3) first evaluates the multiplication and then displays its result.
What to watch for
Quoted text and expressions behave differently. console.log("2 * 3") displays the characters as written.
Code example
console.log("Hello");
console.log(2 * 3);Check your understanding
What appears in the console?
See the example and explanation
- Worked example
- Given
console.log(10 % 4); - Answer
2
The remainder after dividing 10 into groups of 4 is 2.