Client Side Scripting Languages - JavaScript Answers
1. What are arithmetic and logical operators used in JavaScript?
Arithmetic Operators: +, -, *, /, %, ++, --
Logical Operators: && (AND), || (OR), ! (NOT)
2. Write a script that reads an integer and displays whether it is a prime number or not.
let num = parseInt(prompt('Enter a number:'));
let isPrime = true;
if (num <= 1) { isPrime = false; } else {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) { isPrime = false; break; }
if (isPrime) { alert(num + ' is a prime number.'); } else { alert(num + ' is not a prime number.'); }
3. Explain the use of continue statement with the help of example.
The 'continue' statement skips the current iteration.
Example:
for (let i = 1; i <= 5; i++) {
if (i == 3) continue;
console.log(i);
} // Output: 1,2,4,5
4. Explain the feature of JavaScript.
- Lightweight
- Object-oriented
- Platform-independent
- Supports asynchronous operations
- Dynamic typing
5. What is conditional operator in JavaScript?
Client Side Scripting Languages - JavaScript Answers
Ternary Operator Syntax: condition ? exprIfTrue : exprIfFalse;
Example:
let age = 18;
let canVote = (age >= 18) ? 'Yes' : 'No';
6. Explain the terms: property, method, dot syntax.
Property: value in object (object.property)
Method: function in object (object.method())
Dot Syntax: object.property or object.method()
7. Explain six types of values in JavaScript.
- Number
- String
- Boolean
- Object
- Undefined
- Null
8. Write a JavaScript to display welcome message in JavaScript.
alert('Welcome to JavaScript Programming!');
9. Develop a JavaScript to generate Armstrong numbers between 1 to 100.
for (let num = 1; num <= 100; num++) {
let sum = 0, temp = num;
while (temp > 0) {
let digit = temp % 10;
sum += digit ** 3;
temp = Math.floor(temp / 10);
if (sum === num) console.log(num);
Client Side Scripting Languages - JavaScript Answers
10. State the use of dot syntax in JavaScript with suitable example.
Dot syntax allows access to object properties/methods.
Example:
let student = { name: 'Alice' };
console.log(student.name);
11. List and explain Logical operators in JavaScript.
AND (&&): True if both are true
OR (||): True if at least one is true
NOT (!): Inverts boolean value
12. Write a JavaScript that identifies a running browser.
console.log('Browser Name: ' + navigator.appName);
console.log('Browser Version: ' + navigator.appVersion);
13. Write syntax of and explain prompt method in JavaScript with example.
Syntax: prompt(message, default)
Example:
let name = prompt('Enter name:');
alert('Hello, ' + name);
14. Write a JavaScript that displays all properties of window object. Explain the code.
for (let prop in window) {
console.log(prop);
15. Write a JavaScript function that checks whether a passed string is palindrome or not.
function isPalindrome(str) {
Client Side Scripting Languages - JavaScript Answers
let reversed = str.split('').reverse().join('');
return str === reversed;
16. Describe all the tokens of the following statements: document.bgColor document.write()
document: webpage
bgColor: background color (deprecated)
write(): write content to page
17. Differentiate between prompt() and alert() methods.
| Feature | prompt() | alert() |
| --- | --- | --- |
| Purpose | Takes input | Shows message |
| Returns | User value | undefined |