Js Questions
Js Questions
1. Write a program to declare variables using var, let, and const. Try reassigning each
one and observe what happens.
2. Create a variable without initializing it and log its value. Explain what undefined
means in JavaScript.
3. Declare two variables and swap their values without using a third variable.
4. What will happen if you try to access a variable declared with let before
initialization? Write a code snippet to demonstrate this.
5. Declare a global variable and a block-scoped variable. Show how they differ in
accessibility within and outside a block.
Data Types
6. Write a program to check and log the type of the following values: 42, "Hello", true,
null, undefined, and {}.
7. Write a program to convert a number to a string and a string to a number.
8. Demonstrate implicit and explicit type conversion in JavaScript with examples.
9. Write a function that takes any value and checks if it is null or undefined.
10. Create a program that checks if a variable is of type object and if it is an array.
Loops
*
**
***
****
*****
Operators
16. Write a program to demonstrate the difference between == and === with examples.
17. Create a program to calculate the result of (10 + 5) * 2 - 3 using arithmetic operators.
18. Write a program to check if a number is positive, negative, or zero using conditional
(ternary) operators.
Cisco Confidential
19. Demonstrate the use of logical operators (&&, ||, !) in a program that checks if a
number is within a specific range.
20. Create a program to increment and decrement a variable using both ++ and --
operators.
Functions
26. Write a function that validates if a given string is a valid email address.
27. Create a program that simulates a basic calculator. Use a function for each operation
(add, subtract, multiply, divide).
28. Write a program that takes an array of numbers and returns a new array with all
numbers doubled.
29. Create a function that takes a string and returns the string reversed.
30. Write a program to sort an array of numbers in ascending order.
Miscellaneous
Cisco Confidential
40. Write a program to debounce a function call.
41. Create a function to find the frequency of characters in a string.
42. Write a program that flattens a nested array (e.g., [1, [2, [3, 4]]] to [1, 2, 3, 4]).
43. Create a function that returns all unique elements in an array.
44. Write a function to capitalize the first letter of each word in a string.
45. Create a program that finds the GCD (Greatest Common Divisor) of two numbers.
46. Write a program that uses the setTimeout function to log "Hello, World!" after 3
seconds.
47. Create a function that returns the intersection of two arrays.
48. Write a program to count the number of vowels in a given string.
49. Create a function that finds the first non-repeating character in a string.
50. Write a program that removes duplicate values from an array without using Set.
Cisco Confidential
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.
let message = "Hello, World!";
console.log(message);
message = "Hello, JavaScript!";
console.log(message);
Data Types
Cisco Confidential
6. Convert a number to a string and vice versa.
let num = 42;
let str = num.toString();
console.log(typeof str, str);
Loops
Cisco Confidential
let sum = 0;
let i = 1;
while (i <= 100) {
sum += i;
i++;
}
console.log(sum);
Cisco Confidential
let factorial = 1;
for (let i = 1; i <= n; i++) {
factorial *= i;
}
console.log(factorial);
Cisco Confidential