Basics of JavaScript
Basics of JavaScript
JavaScript is a powerful, versatile language used for adding interactivity to websites. If you're just
starting with web development, JavaScript is an essential tool in your toolkit. In this post, we’ll cover
the basics of JavaScript, including how to add JavaScript to your HTML, basic syntax, variables, data
types, functions, and event handling.
You can add JavaScript directly into your HTML file. The simplest way is by using the <script> tag.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Basics</title>
</head>
<body>
<h1>Hello, World!</h1>
<script>
alert("Welcome to JavaScript!");
</script>
</body>
</html>
In this example, the alert() function will display a pop-up message saying "Welcome to JavaScript!" as
soon as the page loads. This method is great for testing simple scripts, but for larger projects, it’s
better to link an external JavaScript file.
Variables store information that can be used later in the program. In JavaScript, you can declare
variables using let, const, or var.
Explanation:
let allows you to reassign the variable later, while const is used for values that shouldn’t
change.
var is an older way of declaring variables but is still used. Generally, use let and const in
modern JavaScript.
Data Types
3. Basic Operations
let x = 5;
let y = 10;
let sum = x + y; // 15
4. Functions
function greet(name) {
Explanation:
Functions can take parameters (like name in this example) and return values.
5. Conditional Statements
} else {
6.Looping Statement
JavaScript provides several types of looping statements, which allow you to execute code multiple
times. Here are the main looping statements:
1. for Loop
for (let i = 0; i < 5; i++) {
console.log("Iteration:", i);
}
2.while loop
let i = 0;
while (i < 5) {
console.log("Iteration:", i);
i++;
let i = 0;
do {
console.log("Iteration:", i);
i++;
console.log(number);
5. for...in Loop
let person = { name: "Alice", age: 25 };
7. Event Handling
<!DOCTYPE html>
<html>
<body>
<script>
function showMessage() {
</script>
</body>
</html>
In this example, clicking the button triggers the showMessage() function, which shows an alert.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<br>
<button onclick="addNumbers()">Add</button>
<h2 id="result"></h2>
<script>
function addNumbers() {
if (isNaN(num1) || isNaN(num2)) {
} else {
</script>
</body>
</html>
Explanation:
1. HTML Structure:
o There are two input fields for entering numbers and a button to trigger the addition.
o The function retrieves the values from the input fields, converts them to numbers
using parseFloat(), and checks if they are valid numbers.
o If valid, it calculates the sum and displays the result in the <h2> element.
<!DOCTYPE html>
<html>
<head>
<title>Prime Numbers</title>
</head>
<body>
<p>Enter a number:</p>
<h2>Prime Numbers:</h2>
<p id="result"></p>
<script>
function isPrime(number) {
if (number % i === 0) {
return false;
}
}
return true;
function findPrimes() {
if (isPrime(i)) {
primes.push(i);
</script>
</body>
</html>
Explanation:
HTML Structure:
A button that, when clicked, calls the findPrimes() function to display prime numbers.
JavaScript Functions:
isPrime(number): Checks if a number is prime by seeing if it has any divisors other than 1 and
itself.
findPrimes(): Retrieves the user's input, iterates through numbers from 2 up to the entered
number, and calls isPrime(i) for each. If isPrime(i) returns true, the number is added to the
primes array.
Finally, the function displays the list of prime numbers in the <p id="result"> element.
University website needs to display a dynamic countdown timer for an upcoming events using
JavaScript. ( University Question)
<!DOCTYPE html>
<html>
<head>
<title>Event Countdown</title>
<style>
body {
text-align: center;
margin-top: 50px;
#countdown {
font-size: 30px;
color: #333;
</style>
</head>
<body>
<p id="countdown">Loading...</p>
<script>
// Set the event date and time (Year, Month (0–11), Day, Hour, Minute, Second)
// Time calculations
document.getElementById("countdown").innerHTML =
days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(countdownTimer);
}, 1000);
</script>
</body>
</html>
Note:The setInterval() function in JavaScript is used to run a block of code repeatedly at a fixed time
interval — in milliseconds.
setInterval(function, delay);
Example:
setInterval(function() {
}, 2000);
This prints "Hello every 2 seconds" to the console every 2000 milliseconds (i.e., every 2 seconds).
Email ID
Mobile Number
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<script>
function validateForm() {
if (!emailPattern.test(email)) {
return false;
if (!mobilePattern.test(mobile)) {
return true;
</script>
</head>
<body>
<label>Email ID:</label><br>
<label>Mobile Number:</label><br>
</form>
</body>
</html>
Explanation:
onsubmit="return validateForm();" runs the JavaScript function when the form is submitted.
Note:
A regular expression (regex) is a pattern used to match character combinations in strings — and it's
perfect for validating formats like email addresses.
Example:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
Par
Meaning
t
Start of the
^
string
[^\s@]
One or more characters for domain extension (e.g., .com)
+