Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

JavaScript_Snippet_Practice_Questions

The document contains a series of JavaScript practice questions focused on variables, data types, and loops. Each question includes code snippets demonstrating key concepts such as variable declaration, type conversion, and loop operations. The exercises are designed to help learners understand fundamental JavaScript programming techniques.

Uploaded by

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

JavaScript_Snippet_Practice_Questions

The document contains a series of JavaScript practice questions focused on variables, data types, and loops. Each question includes code snippets demonstrating key concepts such as variable declaration, type conversion, and loop operations. The exercises are designed to help learners understand fundamental JavaScript programming techniques.

Uploaded by

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

JavaScript Snippet Practice Questions

#### Variables
1. Declare a variable using `let` and assign it a value. Log the value,
reassign it, and log the new value.

```javascript
let message = "Hello, World!";
console.log(message);
message = "Hello, JavaScript!";
console.log(message);
```

2. Swap two numbers using a temporary variable.

```javascript
let a = 5, b = 10;
let temp = a;
a = b;
b = temp;
console.log(a, b);
```

3. Demonstrate hoisting with `var`.

```javascript
console.log(x);
var x = 10;
```

4. Use `const` to declare a variable and try reassigning it.

```javascript
const pi = 3.14;
// pi = 3.15; // Uncomment to see the error
console.log(pi);
```

5. Show the difference between `var`, `let`, and `const` in a loop.

```javascript
for (var i = 0; i < 3; i++) {
console.log(i);
}
console.log(i); // Accessible outside the loop

for (let j = 0; j < 3; j++) {


console.log(j);
}
// console.log(j); // Uncomment to see the error
```

---
#### Data Types
6. Convert a number to a string and vice versa.

```javascript
let num = 42;
let str = num.toString();
console.log(typeof str, str);

let numConverted = Number(str);


console.log(typeof numConverted, numConverted);
```

7. Check the type of a variable using `typeof`.

```javascript
let values = [42, "Hello", true, null, undefined, {}];
values.forEach(value => console.log(typeof value, value));
```

8. Write a program to check if a value is an array.

```javascript
let arr = [1, 2, 3];
console.log(Array.isArray(arr));
```
9. Check if a variable is `null` or `undefined`.

```javascript
let value = null;
console.log(value === null || value === undefined);
```

10. Demonstrate implicit and explicit type conversion.

```javascript
console.log("5" + 5); // Implicit
console.log(Number("5") + 5); // Explicit
```

---

#### Loops
11. Print numbers from 1 to 10 using a `for` loop.

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

12. Calculate the sum of numbers from 1 to 100 using a `while` loop.
```javascript
let sum = 0;
let i = 1;
while (i <= 100) {
sum += i;
i++;
}
console.log(sum);
```

13. Print even numbers from 1 to 20 using a loop.

```javascript
for (let i = 1; i <= 20; i++) {
if (i % 2 === 0) console.log(i);
}
```

14. Reverse an array using a loop.

```javascript
let arr = [1, 2, 3, 4, 5];
let reversed = [];
for (let i = arr.length - 1; i >= 0; i--) {
reversed.push(arr[i]);
}
console.log(reversed);
```

15. Generate a pyramid pattern.

```javascript
let rows = 5;
for (let i = 1; i <= rows; i++) {
console.log("*".repeat(i));
}
```

16. Find the largest number in an array using a loop.

```javascript
let nums = [10, 20, 5, 40, 30];
let max = nums[0];
for (let num of nums) {
if (num > max) max = num;
}
console.log(max);
```

17. Create a new array with doubled values.

```javascript
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(num => num * 2);
console.log(doubled);
```

18. Calculate the factorial of a number using a loop.

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

19. Use `do-while` to repeatedly prompt for input.

```javascript
let input;
do {
input = prompt("Enter a value (type 'exit' to quit):");
} while (input !== "exit");
console.log("Exited");
```

20. Sum the elements of an array using a loop.

```javascript
let nums = [1, 2, 3, 4, 5];
let sum = 0;
for (let num of nums) {
sum += num;
}
console.log(sum);
```

You might also like