Beginner Level JavaScript Interview Questions and Answers
1. What is JavaScript, and what are its key features?
Answer: JavaScript is a high-level, interpreted programming language primarily used to add interactivity to web pages. It can run client-side in browsers or server-side via Node.js.
Key Features:
- Dynamic and weakly typed
- Supports object-oriented, functional, and imperative programming
- Event-driven and asynchronous (callbacks, promises, async/await)
- Runs in browsers and other environments
Example:
document.getElementById("myButton").addEventListener("click", () => alert("Clicked!"));
2. How do you set up a JavaScript development environment?
Answer:
- A text editor (e.g., VS Code)
- A browser (e.g., Chrome) with developer tools
- Optionally, Node.js for server-side JavaScript
- Include JS using
<script>
tags or external.js
files
Example:
<script src="script.js"></script>
3. Where can JavaScript be embedded in an HTML file?
Answer:
<script>
tags in<head>
or<body>
- External files via
<script src="file.js"></script>
- Inline scripts:
<button onclick="myFunction()">Click</button>
Example:
<script>
console.log("Hello, World!");
</script>
4. What are the different ways to output data in JavaScript?
Answer:
console.log()
→ Console outputalert()
→ Popup messagedocument.write()
→ Writes directly to the document (not recommended for dynamic content)
Example:
console.log("Log to console");
alert("This is an alert");
document.write("Written to document");
5. What is the basic syntax of JavaScript?
Answer:
- Case-sensitive
- Semicolons to end statements (optional with ASI)
- Curly braces
{}
for code blocks - Parentheses
()
for function calls and conditions
Example:
let name = "John";
if (name) {
console.log("Hello, " + name);
}
6. What are JavaScript statements?
Answer: Instructions executed by the browser. Examples include variable declarations, loops, and conditionals.
Example:
let x = 5; // Declaration
x = x + 1; // Assignment
7. What are JavaScript comments, and why are they used?
Answer: Comments explain code or prevent execution.
- Single-line:
// Comment
- Multi-line:
/* Comment */
Example:
// Single-line comment
/* Multi-line
comment */
8. What is the difference between var, let, and const?
Answer:
var
: Function-scoped, can be redeclared, hoistedlet
: Block-scoped, can be updated, not redeclared in same scopeconst
: Block-scoped, cannot be updated, must be initialized
Example:
var a = 1;
let b = 2;
const c = 3;
9. What is variable scope in JavaScript?
Answer: Determines where a variable is accessible:
- Global: Accessible everywhere
- Local: Accessible inside functions
- Block:
let
andconst
limited to{}
Example:
let globalVar = "I'm global";
function myFunc() {
let localVar = "I'm local";
console.log(globalVar); // Accessible
console.log(localVar); // Accessible
}
10. What is hoisting in JavaScript?
Answer: Moving declarations to the top of their scope. Only declarations, not initializations, are hoisted.
Example:
console.log(x); // undefined
var x = 5;
11. What are arithmetic operators in JavaScript?
Answer: +
, -
, *
, /
, %
, **
, ++
, --
Example:
let a = 10, b = 5;
console.log(a + b); // 15
console.log(a ** 2); // 100
12. What are comparison operators?
Answer: ==
, ===
, !=
, !==
, >
, <
, >=
, <=
Example:
console.log(5 == "5"); // true
console.log(5 === "5"); // false
13. What is the purpose of logical operators?
Answer: Combine boolean expressions: &&
, ||
, !
Example:
let a = true, b = false;
console.log(a && b); // false
console.log(a || b); // true
14. What are primitive data types in JavaScript?
Answer: String
, Number
, BigInt
, Boolean
, Null
, Undefined
, Symbol
Example:
let str = "Hello";
let num = 42;
let bool = true;
15. What is the difference between null and undefined?
Answer:
null
: Intentional absence of valueundefined
: Variable declared but not assigned
Example:
let x = null;
let y;
16. How does the typeof operator work?
Answer: Returns the type of a variable as a string
Example:
console.log(typeof 42); // "number"
console.log(typeof null); // "object"
17. What is type conversion in JavaScript?
Answer: Changing a value’s type, either explicitly or implicitly
Example:
let num = "123";
console.log(Number(num) + 1); // 124 (explicit)
console.log(num + 1); // "1231" (implicit)
18. What are string methods in JavaScript?
Answer: Methods like toUpperCase()
, toLowerCase()
, slice()
, trim()
, replace()
Example:
let str = "Hello World";
console.log(str.toUpperCase()); // "HELLO WORLD"
console.log(str.slice(0, 5)); // "Hello"
19. What are number methods in JavaScript?
Answer: toFixed()
, toPrecision()
, parseInt()
, parseFloat()
Example:
let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
20. What is the Math object used for?
Answer: Provides constants and functions like Math.PI
, Math.random()
, Math.floor()
Example:
console.log(Math.random()); // Random between 0 and 1
console.log(Math.floor(3.7)); // 3
21. What are booleans in JavaScript?
Answer: Represent true/false values, often used in conditionals
Example:
let isAdult = true;
if (isAdult) console.log("Eligible"); // "Eligible"
22. What is an array in JavaScript?
Answer: An ordered list of values indexed from 0
Example:
let arr = [1, 2, 3];
console.log(arr[0]); // 1
23. What are common array methods?
Answer: push()
, pop()
, shift()
, unshift()
, slice()
, splice()
Example:
let arr = [1, 2];
arr.push(3); // [1,2,3]
console.log(arr.pop()); // 3
24. How does the if statement work in JavaScript?
Answer: Executes a block of code if a condition is true, can use else/else if
Example:
let age = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
25. What is the purpose of a for loop?
Answer: Repeats a block of code a specified number of times
Example:
for (let i = 0; i < 3; i++) {
console.log(i); // 0, 1, 2
}
26. Difference between break and continue in loops
Answer:
break
: exits loop entirelycontinue
: skips current iteration
Example:
for (let i = 0; i < 5; i++) {
if (i === 3) break;
console.log(i); // 0,1,2
}
27. What is block scope in JavaScript?
Answer: Variables declared with let
or const
are limited to the {}
block
Example:
{
let x = 10;
console.log(x); // 10
}
console.log(x); // ReferenceError
28. What is a function declaration in JavaScript?
Answer: Named function using function
keyword, hoisted to the top
Example:
function greet() {
return "Hello!";
}
console.log(greet()); // "Hello!"
29. Difference between parameters and arguments
Answer:
- Parameters: Variables in function definition
- Arguments: Values passed when calling the function
Example:
function add(a, b) { // parameters
return a + b;
}
console.log(add(2, 3)); // 2,3 are arguments
30. What is an object in JavaScript?
Answer: Collection of key-value pairs
Example:
let person = { name: "John", age: 30 };
console.log(person.name); // "John"