0% found this document useful (0 votes)
26 views4 pages

JavaScript Practice Exercises by Prof Deepesh Agarwal

Uploaded by

Suhani Nagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

JavaScript Practice Exercises by Prof Deepesh Agarwal

Uploaded by

Suhani Nagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Practice Exercises for Beginners

Exercise 1: Display Message using console.log()

Write a program to display a welcome message using console.log().

Code:

console.log("Welcome to JavaScript practice!");

Explanation: This prints the message in the browser's console.

Exercise 2: Create a Variable and Print it

Declare a variable to store your name and display it.

Code:

let name = "Deepesh";

console.log("Hello, " + name);

Explanation: Stores a name in a variable and prints it with a greeting.

Exercise 3: Add Two Numbers

Write a program to add two numbers and print the result.

Code:

let a = 5;

let b = 10;

let sum = a + b;

console.log("Sum:", sum);

Explanation: Adds a and b, stores result in 'sum', and logs it.

Exercise 4: Check Even or Odd

Write a program that checks whether a number is even or odd.

Code:

Prof.Deepesh Agarwal
JavaScript Practice Exercises for Beginners

let num = 7;

if (num % 2 == 0) {

console.log("Even");

} else {

console.log("Odd");

Explanation: Uses modulo (%) to check divisibility by 2.

Exercise 5: Use a Loop to Print Numbers

Use a for loop to print numbers from 1 to 5.

Code:

for (let i = 1; i <= 5; i++) {

console.log(i);

Explanation: Loops from 1 to 5 and prints each value.

Exercise 6: Create a Simple Function

Write a function that prints a message.

Code:

function greet() {

console.log("Have a great day!");

greet();

Explanation: Defines a function and then calls it.

Exercise 7: Create an Array and Loop through It

Create an array of fruits and print each fruit.

Prof.Deepesh Agarwal
JavaScript Practice Exercises for Beginners

Code:

let fruits = ["Apple", "Banana", "Mango"];

for (let i = 0; i < fruits.length; i++) {

console.log(fruits[i]);

Explanation: Loops through the array using its length.

Exercise 8: Create an Object and Access Properties

Create a student object and print the name.

Code:

let student = {

name: "Deepesh",

age: 28

};

console.log(student.name);

Explanation: Accesses the 'name' property using dot notation.

Exercise 9: Prompt User Input (in Browser)

Ask the user for their name and greet them.

Code:

let user = prompt("Enter your name:");

alert("Hello, " + user);

Explanation: Uses prompt to get input and alert to show greeting.

Exercise 10: Basic Form Validation

Check if a text field is empty and show alert.

Code:

Prof.Deepesh Agarwal
JavaScript Practice Exercises for Beginners

let name = "";

if (name === "") {

alert("Name is required");

} else {

alert("Submitted");

Explanation: Validates non-empty input.

Prof.Deepesh Agarwal

You might also like