JavaScript Practice Questions PDF
Document Details
Uploaded by Deleted User
Dipika Rijal
Tags
Summary
This document contains JavaScript practice questions. The questions cover various concepts like variables, loops, and object manipulation. The questions are suitable for secondary school-level computer science students.
Full Transcript
JavaScript practice questions S.N. Questions Print your name and interest to the console using console.log(). 1 Output: 2 What is the difference between let, const, and var? Provide examples for each. var is accessible wit...
JavaScript practice questions S.N. Questions Print your name and interest to the console using console.log(). 1 Output: 2 What is the difference between let, const, and var? Provide examples for each. var is accessible within the function where it's defined or globally if not in a function, let accessible only within the block, statement, or expression where it's defined. Whereas, const is used for fixed type of data variables. For e.g.: Output: 3 What is the output of the following code? Explain your answer. let a = 10; if (a > 5) { a = 20; } else { a = 30; } console.log(a); Output: 4 Write a program that determines whether a given number is positive or negative. Output: 5 Write a program that prints numbers from 1 to 100. For multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz," and for numbers which are multiples of both 3 and 5, print "FizzBuzz". Output: 5 Implement a simple calculator using a switch statement for addition, subtraction, multiplication, and division operations. (Optional: Use the prompt method for user input) const operator = prompt('Enter operator ( +, -, * or / ): '); 6 Write a for loop that prints numbers from 11 to 19. Output: Write a while loop that decrements a number from 10 to 1. 7 Output: 8 Write a do...while loop that prints "Hello, World!" at least once. Output: Declares an array of numbers and prints their sum. 9 Output: 10 Add a new element to the array of numbers using the push method and print it. Output: 11 Write a JavaScript code that removes the first element from an array. Output: 12 What is the output of the following code? Explain your answer. let obj = {a: 1, b: 2} ; obj.a = 5;................ console.log(obj);.... Output: 13 Given an object person = {name: 'John', age: 30}, write a for...in loop that prints each property and its value. Output: Add a new property to an object person and print it. 14 Output: 15 Delete a property of the object person and print it. Output: What is the difference between an array and an object in JavaScript? 16 Output: 17 Write a JavaScript program that defines a function that takes a number as input and logs whether it is even or odd. Output: 18 Write a JavaScript program that defines a function that takes an array of numbers as input and returns the sum of its elements. Output: Output: 19 Write a JavaScript program that takes an array of numbers as input and returns the second-largest number in the array. The program should use a loop to find the largest and second-largest numbers. Output: 20 Write a JavaScript program that takes a string as input and returns an object with the count of each unique character in the string. The program should use a loop to iterate through the string and another loop to count the occurrences of each character. Output: