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

Important Questions JavaScript

Uploaded by

Bhaskar Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Important Questions JavaScript

Uploaded by

Bhaskar Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Important Questions and Model Answers for JavaScript (22519)

Unit I: Basics of JavaScript Programming

1. Explain any two features of JavaScript.


- Lightweight: Minimal syntax, suitable for client-side scripting.
- Event-driven: Can handle user actions dynamically.

2. Write a JavaScript to display a Welcome message.


```javascript
alert("Welcome to JavaScript Programming!");
```

3. Write a JavaScript program to check whether a number is even or odd.


```javascript
let num = 5;
if (num % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
```

4. Write a JavaScript to display squares of numbers from 1 to 10 using a loop.


```javascript
for (let i = 1; i <= 10; i++) {
console.log(i * i);
}
```

Unit II: Array, Function, and String

1. Define an array and explain how to declare it.


An array is a collection of elements stored at contiguous memory locations.
Declaration:
```javascript
let arr = [1, 2, 3, 4];
```

2. Write a JavaScript program to reverse a string.


```javascript
let str = "JavaScript";
let reversed = str.split('').reverse().join('');
console.log(reversed); // Output: tpircSavaJ
```

3. Explain the use of `push()` and `pop()` methods in JavaScript.


- `push()`: Adds an element to the end of the array.
- `pop()`: Removes the last element from the array.
Example:
```javascript
let arr = [1, 2, 3];
arr.push(4); // [1, 2, 3, 4]
arr.pop(); // [1, 2, 3]
```

Unit III: Form and Event Handling

1. What is an event handler? Give an example.


An event handler is a function that executes when a specific event occurs.
Example:
```javascript
button.onclick = function() {
alert("Button clicked!");
};
```

2. Write a JavaScript program to validate a form with a name and email field.
```javascript
function validateForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
if (name === "" || email === "") {
alert("Name and Email cannot be empty.");
} else {
alert("Form submitted.");
}
}
```

Unit IV: Cookies and Browser Data

1. Write a JavaScript program to create, read, and delete a cookie.


```javascript
// Create a cookie
document.cookie = "username=JohnDoe; expires=Fri, 31 Dec 2023 23:59:59 UTC;";
// Read the cookie
console.log(document.cookie);
// Delete the cookie
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
```

2. Explain the `window.open()` method with an example.


The `window.open()` method opens a new browser window.
Example:
```javascript
let newWindow = window.open("https://www.example.com", "_blank",
"width=500,height=500");
```

Unit V: Regular Expression, Rollover, and Frames

1. Write a regular expression to validate an email address.


```javascript
let emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
console.log(emailRegex.test("[email protected]")); // true
```

2. Write a JavaScript program to implement a rollover effect for images.


```html
<img src="image1.jpg" onmouseover="this.src='image2.jpg';"
onmouseout="this.src='image1.jpg';">
```

Unit VI: Menus, Navigation, and Web Page Protection

1. Write a JavaScript program to create a floating menu.


```html
<div style="position:fixed; top:10px; left:10px;">Floating Menu</div>
```

2. Write a JavaScript program to disable right-click on a webpage.


```javascript
document.addEventListener("contextmenu", function(e) {
e.preventDefault();
alert("Right-click is disabled");
});
```

You might also like