0% found this document useful (0 votes)
15 views

Javascript If Else Questions

javascript

Uploaded by

catkelvis
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)
15 views

Javascript If Else Questions

javascript

Uploaded by

catkelvis
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
You are on page 1/ 9

20 Beginner-Level JavaScript If-Else Questions

Q1. Write a JavaScript program to check if a number is positive or negative.

let num = 5;

if (num > 0) {

console.log("Positive");

} else {

console.log("Negative");

Output: Positive

Q2. Write a JavaScript program to check if a number is even or odd.

let num = 4;

if (num % 2 === 0) {

console.log("Even");

} else {

console.log("Odd");

Output: Even

Q3. Write a JavaScript program to check if a person is eligible to vote based on age.

let age = 18;

if (age >= 18) {

console.log("Eligible to vote");
} else {

console.log("Not eligible to vote");

Output: Eligible to vote

Q4. Write a JavaScript program to check if a number is divisible by 5.

let num = 25;

if (num % 5 === 0) {

console.log("Divisible by 5");

} else {

console.log("Not divisible by 5");

Output: Divisible by 5

Q5. Write a JavaScript program to find the larger of two numbers.

let a = 10;

let b = 20;

if (a > b) {

console.log("a is larger");

} else {

console.log("b is larger");

Output: b is larger
Q6. Write a JavaScript program to check if a number is a multiple of 3.

let num = 9;

if (num % 3 === 0) {

console.log("Multiple of 3");

} else {

console.log("Not a multiple of 3");

Output: Multiple of 3

Q7. Write a JavaScript program to check if a number is zero, positive or negative.

let num = 0;

if (num > 0) {

console.log("Positive");

} else if (num < 0) {

console.log("Negative");

} else {

console.log("Zero");

Output: Zero

Q8. Write a JavaScript program to check if a number is a perfect square.

let num = 16;

if (Math.sqrt(num) % 1 === 0) {
console.log("Perfect Square");

} else {

console.log("Not a Perfect Square");

Output: Perfect Square

Q9. Write a JavaScript program to check if a character is a vowel or a consonant.

let char = 'a';

if (['a', 'e', 'i', 'o', 'u'].includes(char)) {

console.log("Vowel");

} else {

console.log("Consonant");

Output: Vowel

Q10. Write a JavaScript program to check if a number is within a range (10-20).

let num = 15;

if (num >= 10 && num <= 20) {

console.log("Within Range");

} else {

console.log("Out of Range");

Output: Within Range


Q11. Write a JavaScript program to check if a year is a leap year.

let year = 2024;

if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {

console.log("Leap Year");

} else {

console.log("Not a Leap Year");

Output: Leap Year

Q12. Write a JavaScript program to check if a person is eligible for senior citizen benefits based on

age (60+).

let age = 65;

if (age >= 60) {

console.log("Eligible for senior citizen benefits");

} else {

console.log("Not eligible for senior citizen benefits");

Output: Eligible for senior citizen benefits

Q13. Write a JavaScript program to check if a number is a prime number.

let num = 7;

let isPrime = true;

if (num === 1) {
isPrime = false;

} else {

for (let i = 2; i < num; i++) {

if (num % i === 0) {

isPrime = false;

break;

if (isPrime) {

console.log("Prime Number");

} else {

console.log("Not a Prime Number");

Output: Prime Number

Q14. Write a JavaScript program to find the largest of three numbers.

let a = 10;

let b = 20;

let c = 15;

if (a > b && a > c) {

console.log("a is the largest");

} else if (b > a && b > c) {

console.log("b is the largest");

} else {
console.log("c is the largest");

Output: b is the largest

Q15. Write a JavaScript program to check if a number is between 50 and 100.

let num = 75;

if (num >= 50 && num <= 100) {

console.log("Between 50 and 100");

} else {

console.log("Not between 50 and 100");

Output: Between 50 and 100

Q16. Write a JavaScript program to check if a number is positive, negative, or zero.

let num = -10;

if (num > 0) {

console.log("Positive");

} else if (num < 0) {

console.log("Negative");

} else {

console.log("Zero");

Output: Negative
Q17. Write a JavaScript program to check if a letter is uppercase or lowercase.

let letter = 'G';

if (letter === letter.toUpperCase()) {

console.log("Uppercase");

} else {

console.log("Lowercase");

Output: Uppercase

Q18. Write a JavaScript program to check if a number is a multiple of both 3 and 5.

let num = 15;

if (num % 3 === 0 && num % 5 === 0) {

console.log("Multiple of both 3 and 5");

} else {

console.log("Not a multiple of both 3 and 5");

Output: Multiple of both 3 and 5

Q19. Write a JavaScript program to check if a number is a multiple of 10.

let num = 100;

if (num % 10 === 0) {

console.log("Multiple of 10");

} else {
console.log("Not a multiple of 10");

Output: Multiple of 10

Q20. Write a JavaScript program to check if a character is an alphabet or not.

let char = '9';

if ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')) {

console.log("Alphabet");

} else {

console.log("Not an Alphabet");

Output: Not an Alphabet

You might also like