SimpleTuts.com

Variables, Data Types, and Operators

Understanding Variables, Data Types, and Operators in JavaScript

1. What is a Variable?

A variable is a container used to store data that can be used later in a program.

Example:


let name = "John";

Here, name is a variable storing the value "John".

2. Types of Variables in JavaScript

var

Old way of declaring variables.


var age = 20;

let

Modern way. Value can change.


let score = 50;
score = 60;

const

Used for constant values.


const country = "India";

3. Data Types in JavaScript

String

Text values.


let studentName = "Rahul";

Number

Numeric values.


let marks = 95;

Boolean

True or false values.


let isPassed = true;

4. Real Life Example: Greeting Application

This example greets the user when they enter their name.

index.html


<!DOCTYPE html>
<html>
<head>
<title>Greeting App</title>
</head>
<body>

<h2>Greeting Application</h2>

<input type="text" id="name" placeholder="Enter your name">
<button onclick="greetUser()">Greet</button>

<p id="message"></p>

<script src="app.js"></script>

</body>
</html>
Run Code

app.js


function greetUser() {
    let name = document.getElementById("name").value;
    document.getElementById("message").innerText = "Hello " + name + "! Welcome to JavaScript.";
}
Run Code

5. How This Example Works

  1. User enters their name in the input box.
  2. When the button is clicked, the function greetUser() runs.
  3. JavaScript reads the value from the input field.
  4. The message is displayed on the page.

6. Practice Exercise

Create a small application that:

7. Operators

Arithmetic Operators


let a = 10;
let b = 5;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);

Comparison Operators


console.log(a > b);
console.log(a < b);
console.log(a == b);

8. Real Life Example: Student Marks Calculator

This application calculates the total marks and percentage.

index.html


<!DOCTYPE html>
<html>
<head>
<title>Marks Calculator</title>
</head>
<body>

<h2>Student Marks Calculator</h2>

<input type="number" id="m1" placeholder="Subject 1"><br><br>
<input type="number" id="m2" placeholder="Subject 2"><br><br>
<input type="number" id="m3" placeholder="Subject 3"><br><br>

<button onclick="calculate()">Calculate</button>

<h3 id="result"></h3>

<script src="app.js"></script>

</body>
</html>
Run Code

app.js


function calculate(){

let m1 = Number(document.getElementById("m1").value);
let m2 = Number(document.getElementById("m2").value);
let m3 = Number(document.getElementById("m3").value);

let total = m1 + m2 + m3;
let percentage = total / 3;

let result = document.getElementById("result");

result.innerText = "Total Marks: " + total + " | Percentage: " + percentage + "%";

}
Run Code

9. How This Example Works

  1. User enters marks for three subjects.
  2. JavaScript stores the values in variables.
  3. The program calculates total and percentage.
  4. The result is displayed on the webpage.

10. Practice Exercise

Create a simple Shopping Bill Calculator.

Inputs:

Output:

11. What Students Learned

Next Chapter

Chapter 3: Conditional Statements (if, else, switch) with a Real-Life Example: Age Eligibility Checker