🟨 JavaScript tutorial for beginners · Class 8 · Lesson 1 of 18 · Free
JavaScript Introduction – Your First JavaScript Program
⏱ About 25 minutes · ▶ run real JavaScript here — nothing to install · no sign-in
In this lesson you will
- Write and run your first JavaScript program with console.log()
- Use numbers, text and true/false values in JavaScript
- Change a live web page with a few lines of JavaScript
Riya's school website shows the same boring notice every day. Her friend says, "With JavaScript, the page can greet each student and change when you click." Today you write your first lines of JavaScript and make a real page react.
What is JavaScript and how do I write my first JavaScript program?
Every web page is made of three parts. HTML is the content, like the walls of a house. CSS is the style, like the paint. JavaScript is the action, like the switches and fans.
JavaScript runs inside every web browser, so you need nothing to install. The command console.log() prints a message in the console. The console is the box under the code where you see results.
Each line is one instruction. JavaScript runs them from top to bottom. A line usually ends with a semicolon ;
Hello, World!Namaste from JavaScript!I am in Class 8.
💡 Try it: change the text inside the quotes to your own name and press ▶ Run.
How does console.log print numbers and text in JavaScript?
Text must be inside quotes. JavaScript calls text a string. Numbers have no quotes, and JavaScript can do maths with them.
Look closely: "2 + 3" in quotes prints exactly as written. But 2 + 3 without quotes is calculated first. You can also print many things at once by separating them with commas.
The symbols are + (add), - (subtract), * (multiply) and / (divide).
▶console.log("2 + 3");2console.log(2 + 3);3console.log(45 * 4);4console.log(100 / 8);5console.log("Runs scored:", 87);
No variables yet.
▌✏️ Change this code and run it yourself
2 + 3518012.5Runs scored: 87
💡 Try it: work out how many minutes are in a day with 24 * 60.
How do I store a value in a JavaScript variable?
A variable is a named box that holds a value. You make one with let. Later you can put a new value in the same box.
Use const when the value should never change, like the number of days in a week. Use let when it will change, like a cricket score.
Names are case-sensitive: score and Score are two different boxes. Lines starting with // are comments. JavaScript skips them; they are notes for humans.
▶const player = "Virat";2let runs = 40;3console.log(player, "has", runs, "runs");45// He hits a four!6runs = runs + 4;7console.log(player, "has", runs, "runs");89// And a six!10runs = runs + 6;11console.log(player, "has", runs, "runs");
No variables yet.
▌✏️ Change this code and run it yourself
Virat has 40 runsVirat has 44 runsVirat has 50 runs
💡 Watch the runs box change in the animation. Lesson 2 covers variables in detail.
What are strings, numbers and booleans in JavaScript?
JavaScript has a few basic kinds of values. A string is text in quotes: "Pune". A number is any number: 42 or 3.5. A boolean is just true or false, like a light switch.
Comparisons give you booleans. The > sign asks "is it bigger?" and === asks "is it exactly equal?".
The + sign joins two strings together. That is how you build a sentence out of pieces.
Pune78trueHello from Punefalse
💡 Try it: change marks to 20 and see what passed becomes.
How does JavaScript change a web page?
This box has a real web page. The HTML makes a heading and a button. The JavaScript finds them and adds an action.
document.querySelector() finds a part of the page. addEventListener("click", ...) says "when this is clicked, run this code". textContent is the text inside it.
Do not worry about every word yet. Lessons 4 and 5 explain all of it. Just press ▶ Run, then click the button on the page.
📄 HTML of the page (tap to see / edit)
Page ready. Click the button.
💡 Try it: change the Namaste message and run again.
How do I ask the user a question in JavaScript?
prompt() shows a small box where the user can type an answer. The answer comes back as a string, and you can store it in a variable.
alert() shows a pop-up message. Real websites use these rarely, but they are a quick way to talk to the user while you learn.
Here the answer "Aarav" is filled in for you. On your own computer, the browser would ask you.
💬 What is your name? → AaravHello, Aarav!🔔 Welcome to JavaScript, Aarav
🤔 Predict the output
Think like JavaScript: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
console.log("4 + 4");Question 2. What does this print?
let score = 10; score = score + 5; console.log(score);
Question 3. What does this print?
console.log("Total:", 20 * 3);🧩 Your challenge
Make a scorecard for a cricket player. Store the player's name, runs and balls faced in variables. Then print the name, the runs, and the strike rate (runs * 100 / balls). For Rohit with 60 runs off 40 balls the console should show: Player: Rohit Runs: 60 Strike rate: 150
Scorecard Player: Rohit Runs: 60 Strike rate: 150
⚠️ Common mistakes (and how to fix them)
❌ Wrong
Console.log("Hello");ReferenceError: Console is not defined
✅ Right
console.log("Hello");JavaScript is case-sensitive. console must be all small letters; Console with a capital C is a different name that does not exist.
❌ Wrong
console.log(Hello);
ReferenceError: Hello is not defined
✅ Right
console.log("Hello");Without quotes, JavaScript thinks Hello is the name of a variable. Put text inside quotes to make it a string.
❌ Wrong
const runs = 40; runs = runs + 4; console.log(runs);
TypeError: Assignment to constant variable.
✅ Right
let runs = 40; runs = runs + 4; console.log(runs);
A const box can never get a new value. Use let for values that change, like a score.
📌 Remember
- JavaScript is the language that makes web pages do things; it runs in every browser.
- console.log() prints a message in the console — your main tool for seeing results.
- Text (strings) goes in quotes; numbers do not, and JavaScript can calculate with them.
- let makes a variable that can change; const makes one that cannot.
- JavaScript is case-sensitive: console is not Console.
- With querySelector and addEventListener, JavaScript can change a live page when you click.
Finished the quiz and the challenge?
Questions students ask
Is JavaScript the same as Java?
No. They are two different languages with similar names. Java is often used for big apps and Android. JavaScript runs in web browsers and makes pages interactive.
Is JavaScript hard to learn for a Class 8 student?
No. You can write a working line on day one, like you did here. If you learnt some Python before, many ideas (variables, printing, maths) are the same. Practise a little every day.
Do I need to install anything to run JavaScript?
No. Every web browser already runs JavaScript. On this page you just press ▶ Run. On a computer you can also open the browser's console with F12 and type code there.
What is console.log used for in JavaScript?
console.log() prints values in the console. Programmers use it to see results and to find mistakes in their code. Visitors to a website do not see it on the page.