Introductions
JavaScript is a programming language used to make websites interactive and dynamic. Before moving to advanced concepts, beginners should understand basic JavaScript syntax, variables, values, expressions, and simple calculations. This chapter starts with very easy examples and gradually introduces practical problems. Each question includes a solution, output, and simple explanation so that beginners can understand not only what the code does, but also how it works. JavaScript Basics practice questions with solutions help to understand the concepts.
Question 1: Display a Message
Problem
Write a JavaScript program to display Hello JavaScript! in the console.
Solution
console.log("Hello JavaScript!");
Output
Hello JavaScript!
Step-by-step Explanation
console.log()is used to display information in the browser console."Hello JavaScript!"is a string because it is written inside quotation marks.- When the code runs, JavaScript displays the message in the console.
Question 2: Store and Display a Name
Problem
Create a variable called name, store a person’s name in it, and display the name in the console.
Solution
letname="Rishabh";console.log(name);
Output
Rishabh
Step-by-step Explanation
letis used to create a variable.nameis the name of the variable."Rishabh"is stored inside the variable.console.log(name)displays the value stored inname.
You can replace "Rishabh" with any name.
Question 3: Add Two Numbers
Problem
Create two variables containing 20 and 30, then calculate and display their sum.
Solution
letnumber1=20;letnumber2=30;letsum=number1+number2;console.log(sum);
Output
50
Step-by-step Explanation
number1stores the value20.number2stores the value30.- The
+operator adds the two numbers. - The result is stored in the
sumvariable. console.log(sum)displays50.
Question 4: Calculate the Total Price
Problem
A student buys a notebook for ₹50 and a pen for ₹20. Write a JavaScript program to calculate the total price.
Solution
letnotebook=50;letpen=20;lettotal=notebook+pen;console.log(total);
Output
70
Step-by-step Explanation
- The
notebookvariable stores50. - The
penvariable stores20. - The
+operator adds both prices. - The result is stored in
total. - The total price is
70.
Question 5: Calculate Age Next Year
Problem
A student is 14 years old. Write a JavaScript program to calculate the student’s age next year.
Solution
letage=14;letnextYearAge=age+1;console.log(nextYearAge);
Output
15
Step-by-step Explanation
- The
agevariable stores14. age + 1adds one year to the current age.- The result is stored in
nextYearAge. - The program displays
15.
Question 6: Calculate the Area of a Rectangle
Problem
A rectangle has a length of 10 cm and a width of 5 cm. Write a JavaScript program to calculate its area.
Solution
letlength=10;letwidth=5;letarea=length*width;console.log(area);
Output
50
Step-by-step Explanation
The formula for the area of a rectangle is:
Area = Length × Width
Using the given values:
10 × 5 = 50
In JavaScript, the * operator is used for multiplication.
Question 7: Create a Greeting Message
Problem
Create a variable containing a person’s name and display a greeting message such as Welcome, Rishabh!.
Solution
letname="Rishabh";console.log("Welcome, "+name+"!");
Output
Welcome, Rishabh!
Step-by-step Explanation
- The
namevariable stores"Rishabh". - The
+operator joins text together. "Welcome, "is joined with the value stored inname."!"is added at the end.- Joining strings together is called string concatenation.
Question 8: Calculate Total Marks
Problem
A student scores 80 marks in English, 75 marks in Mathematics, and 90 marks in Science. Write a JavaScript program to calculate the total marks.
Solution
letenglish=80;letmathematics=75;letscience=90;lettotal=english+mathematics+science;console.log(total);
Output
245
Step-by-step Explanation
englishstores80.mathematicsstores75.sciencestores90.- The three values are added together.
- The total is
245.
The calculation is:
80 + 75 + 90 = 245
Question 9: Calculate Average Marks
Problem
A student gets 80, 70, and 90 marks in three subjects. Write a JavaScript program to calculate the average marks.
Solution
letmarks1=80;letmarks2=70;letmarks3=90;lettotal=marks1+marks2+marks3;letaverage=total/3;console.log(average);
Output
80
Step-by-step Explanation
First, calculate the total marks:
80 + 70 + 90 = 240
There are three subjects, so divide the total by 3:
240 / 3 = 80
The / operator is used for division.
Therefore, the average marks are 80.
Question 10: Calculate a Discounted Price
Problem
A product costs ₹1,000 and has a 10% discount. Write a JavaScript program to calculate the discount amount and final price.
Solution
letprice=1000;letdiscount=10;letdiscountAmount=price*discount/100;letfinalPrice=price-discountAmount;console.log(discountAmount);console.log(finalPrice);
Output
100900
Step-by-step Explanation
First, calculate the discount amount:
1000 × 10 / 100 = 100
The discount amount is ₹100.
Now subtract the discount from the original price:
1000 - 100 = 900
Therefore, the final price is ₹900.
Key Takeaways
- JavaScript is used to add logic and dynamic behavior to websites.
console.log()displays information in the browser console.- Variables are used to store values.
letis used to declare a variable.- Strings are used to store text.
- Numbers can be used directly in mathematical calculations.
- The
+operator performs addition and can also join strings. - The
-operator performs subtraction. - The
*operator performs multiplication. - The
/operator performs division. - Simple calculations are a good way to understand JavaScript expressions.
- Practicing small problems helps build a strong foundation for advanced JavaScript.
FAQs
1. What is JavaScript?
JavaScript is a programming language commonly used to make websites interactive and dynamic.
2. What is console.log() in JavaScript?
console.log() is used to display messages, values, and calculation results in the browser’s developer console.
3. What is a variable in JavaScript?
A variable is a named container used to store a value, such as a number or string.
4. What is the purpose of let in JavaScript?
let is used to declare a variable. The value stored in a let variable can be changed later.
5. Can JavaScript perform mathematical calculations?
Yes. JavaScript can perform addition, subtraction, multiplication, division, percentages, and many other mathematical operations.
6. What is a string in JavaScript?
A string is a sequence of characters used to represent text. For example, "Hello" and "JavaScript" are strings.
7. Is JavaScript easy for beginners?
Yes. JavaScript can be learned step by step. Starting with variables, data types, operators, conditions, loops, arrays, and functions provides a strong foundation.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
