Javascript Practice – Questions & Answers
Beginner Level
Q1. What is JavaScript?
JavaScript is a scripting language used to create dynamic web pages.
Q2. How to include JavaScript in HTML?
<script src="app.js"></script>
Q3. Print message to console.
console.log("Hello");Q4. Declare a variable.
let x = 10;
Q5. Difference between var, let, const.
var = function scope, let = block scope, const = constant.
Q6. JavaScript data types.
String, Number, Boolean, Object, Undefined, Null
Q7. Create an array.
let arr = [1,2,3];
Q8. Access array element.
arr[0];
Q9. Create an object.
let user = {name:"Vijay", age:25};Q10. Write an if statement.
if(age > 18){
console.log("Adult");
}
Q11. Write a for loop.
for(let i=0;i<5;i++){
console.log(i);
}
Q12. Function syntax.
function greet(){
console.log("Hi");
}
Q13. Arrow function.
const add = (a,b) => a+b;
Q14. Alert message.
alert("Welcome");Q15. Prompt input.
prompt("Enter name");Q16. Confirm dialog.
confirm("Are you sure?");Q17. Get element by ID.
document.getElementById("box");Q18. Change text content.
el.textContent = "Hello";
Q19. Change HTML content.
el.innerHTML = "<b>Hi</b>";
Q20. Comment in JavaScript.
// Single line comment
Q21. typeof operator.
typeof 10;
Q22. Convert string to number.
Number("10");Q23. Convert number to string.
String(10);
Q24. Math random.
Math.random();
Q25. Check array length.
arr.length;
Intermediate Level
Q26. What is DOM?
DOM represents HTML as a tree of objects.
Q27. Query selector.
document.querySelector(".box");Q28. Add event listener.
btn.addEventListener("click", ()=>{
alert("Clicked");
});
Q29. Change CSS using JS.
box.style.color = "red";
Q30. setTimeout.
setTimeout(()=>{},1000);Q31. setInterval.
setInterval(()=>{},1000);Q32. Array push.
arr.push(5);
Q33. Array pop.
arr.pop();
Q34. map() method.
arr.map(x => x*2);
Q35. filter() method.
arr.filter(x => x > 2);
Q36. reduce() method.
arr.reduce((a,b)=>a+b);
Q37. JSON stringify.
JSON.stringify(obj);
Q38. JSON parse.
JSON.parse(jsonData);
Q39. LocalStorage set.
localStorage.setItem("user","Vijay");Q40. LocalStorage get.
localStorage.getItem("user");Q41. SessionStorage.
sessionStorage.setItem("id",1);Q42. Ternary operator.
age>18 ? "Yes":"No";
Q43. Default parameter.
function test(x=5){}Q44. Spread operator.
let b = [...a];
Q45. Rest parameter.
function sum(...n){}Q46. Destructuring.
let {name,age} = user;Q47. forEach loop.
arr.forEach(x=>console.log(x));
Q48. Includes method.
arr.includes(3);
Q49. String length.
name.length;
Q50. Trim string.
name.trim();
Q51. Date object.
new Date();
Q52. Get year.
new Date().getFullYear();
Q53. ParseInt.
parseInt("10");Q54. NaN check.
isNaN(x);
Q55. Window object.
Global browser object.
Q56. History back.
history.back();
Q57. Location reload.
location.reload();
Q58. Event object.
Contains event details.
Q59. Prevent default.
event.preventDefault();
Q60. Stop propagation.
event.stopPropagation();
Advanced & Interview Level
Q61. What is closure?
Function with access to outer scope variables.
Q62. Hoisting.
Variables/functions moved to top.
Q63. Call stack.
Tracks function execution.
Q64. Callback function.
Function passed as argument.
Q65. Promise.
new Promise((res,rej)=>res());
Q66. Promise states.
Pending, Fulfilled, Rejected
Q67. Async function.
async function load(){}Q68. Await keyword.
await fetch(url);
Q69. Fetch API.
fetch(url).then(res=>res.json());
Q70. Try catch.
try{}catch(e){}
Q71. Event bubbling.
Child → Parent
Q72. Event capturing.
Parent → Child
Q73. Event delegation.
Handle events via parent.
Q74. Debounce.
Delays execution.
Q75. Throttle.
Limits execution rate.
Q76. this keyword.
Refers to current context.
Q77. call() method.
func.call(obj);
Q78. apply() method.
func.apply(obj,[]);
Q79. bind() method.
func.bind(obj);
Q80. Prototype.
Object inheritance mechanism.
Q81. Class syntax.
class User{
constructor(){}
}
Q82. Inheritance.
class A extends B{}Q83. Strict mode.
"use strict";
Q84. Module export.
export default func;
Q85. Module import.
import func from "./file.js";
Q86. Optional chaining.
user?.address?.city;
Q87. Nullish coalescing.
x = val ?? 0;
Q88. Web storage types.
Local & Session Storage
Q89. Memory leak.
Unused memory not released.
Q90. Shallow vs Deep copy.
Reference vs value copy.
Q91. JSON vs Object.
JSON is string format.
Q92. set vs map.
Set = unique values, Map = key-value.
Q93. WeakMap.
Garbage collectible keys.
Q94. requestAnimationFrame.
requestAnimationFrame(fn);
Q95. Web Workers.
Background thread execution.
Q96. Service Worker.
Offline & caching.
Q97. CORS.
Cross-Origin Resource Sharing.
Q98. Single-threaded.
JS runs on single thread.
Q99. Event loop.
Handles async operations.
Q100. JavaScript best practices.
Use ES6+, modular code, async handling.
No comments:
Post a Comment