JavaScriptLesson 1 of 10Jump to practice
What is JavaScript?
JavaScript makes web pages respond to people. Run your first script and print to the console.
HTML is the structure, CSS is the look, and JavaScript is the behavior. It is a programming language that runs in the browser and can change the page while someone is using it.
Your first script
JavaScript goes inside a <script> element. console.log() prints a message to the console, a log that developers use to check what their code is doing. In these examples the console appears under the result.
<script>
console.log("Hello from JavaScript!");
console.log(2 + 3);
</script>Changing the page
JavaScript can find an element and change it. Here, clicking the button runs a function, a named group of instructions.
<p id="message">Nobody has clicked yet.</p>
<button onclick="sayHi()">Click me</button>
<script>
let clicks = 0;
function sayHi() {
clicks = clicks + 1;
document.getElementById("message").textContent =
"You clicked " + clicks + " times.";
console.log("Click number", clicks);
}
</script>What happened
document.getElementById("message")finds the element whoseidismessage..textContent = ...replaces the text inside it.onclick="sayHi()"tells the button to run thesayHifunction when it is clicked.
Practice
Check what you just learned. Fill in the missing code, then answer the quiz.
Exercise 1Solved
Print the message to the console.
("Hello!");It is an object name, a dot, and a function name.
Exercise 2Solved
JavaScript goes inside which element?
<> console.log("Running!"); </>Exercise 3Solved
Find the element whose id is
title.const heading = document.("title");Read it as get element by id, written in camelCase.
Finish the practice to complete this lesson.
Sign in to keep your progress on any device.