JavaScript Basics Practice Questions with Solutions

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

  1. console.log() is used to display information in the browser console.
  2. "Hello JavaScript!" is a string because it is written inside quotation marks.
  3. 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

  1. let is used to create a variable.
  2. name is the name of the variable.
  3. "Rishabh" is stored inside the variable.
  4. console.log(name) displays the value stored in name.

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

  1. number1 stores the value 20.
  2. number2 stores the value 30.
  3. The + operator adds the two numbers.
  4. The result is stored in the sum variable.
  5. console.log(sum) displays 50.

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

  1. The notebook variable stores 50.
  2. The pen variable stores 20.
  3. The + operator adds both prices.
  4. The result is stored in total.
  5. 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

  1. The age variable stores 14.
  2. age + 1 adds one year to the current age.
  3. The result is stored in nextYearAge.
  4. 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

  1. The name variable stores "Rishabh".
  2. The + operator joins text together.
  3. "Welcome, " is joined with the value stored in name.
  4. "!" is added at the end.
  5. 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

  1. english stores 80.
  2. mathematics stores 75.
  3. science stores 90.
  4. The three values are added together.
  5. 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.
  • let is 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.

Scroll to Top