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!";
[Link](message);
message = "Hello, JavaScript!";
[Link](message);
```
2. Swap two numbers using a temporary variable.
```javascript
let a = 5, b = 10;
let temp = a;
a = b;
b = temp;
[Link](a, b);
```
3. Demonstrate hoisting with `var`.
```javascript
[Link](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
[Link](pi);
```
5. Show the difference between `var`, `let`, and `const` in a loop.
```javascript
for (var i = 0; i < 3; i++) {
[Link](i);
}
[Link](i); // Accessible outside the loop
for (let j = 0; j < 3; j++) {
[Link](j);
}
// [Link](j); // Uncomment to see the error
```
---
#### Data Types
6. Convert a number to a string and vice versa.
```javascript
let num = 42;
let str = [Link]();
[Link](typeof str, str);
let numConverted = Number(str);
[Link](typeof numConverted, numConverted);
```
7. Check the type of a variable using `typeof`.
```javascript
let values = [42, "Hello", true, null, undefined, {}];
[Link](value => [Link](typeof value, value));
```
8. Write a program to check if a value is an array.
```javascript
let arr = [1, 2, 3];
[Link]([Link](arr));
```
9. Check if a variable is `null` or `undefined`.
```javascript
let value = null;
[Link](value === null || value === undefined);
```
10. Demonstrate implicit and explicit type conversion.
```javascript
[Link]("5" + 5); // Implicit
[Link](Number("5") + 5); // Explicit
```
---
#### Loops
11. Print numbers from 1 to 10 using a `for` loop.
```javascript
for (let i = 1; i <= 10; i++) {
[Link](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++;
}
[Link](sum);
```
13. Print even numbers from 1 to 20 using a loop.
```javascript
for (let i = 1; i <= 20; i++) {
if (i % 2 === 0) [Link](i);
}
```
14. Reverse an array using a loop.
```javascript
let arr = [1, 2, 3, 4, 5];
let reversed = [];
for (let i = [Link] - 1; i >= 0; i--) {
[Link](arr[i]);
}
[Link](reversed);
```
15. Generate a pyramid pattern.
```javascript
let rows = 5;
for (let i = 1; i <= rows; i++) {
[Link]("*".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;
}
[Link](max);
```
17. Create a new array with doubled values.
```javascript
let numbers = [1, 2, 3, 4, 5];
let doubled = [Link](num => num * 2);
[Link](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;
}
[Link](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");
[Link]("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;
}
[Link](sum);
```