1. How is JavaScript different from Java?
Ans:
Java is a statically typed, compiled and object oriented language used for building large-scale applications. JavaScript is a dynamically typed interpreted language mainly used for creating interactive web pages. Java programs run on the JVM whereas JavaScript runs in browsers or Node.js. Despite some similar syntax, their purposes and development environments are entirely different.
2. What types of data are available in JavaScript?
Ans:
JavaScript supports primitive types like String, Number, Boolean, Null, Undefined, BigInt and Symbol. It also supports reference types such as Objects, Arrays and Functions. These data types allow developers to manage both simple values and complex structures effectively.
3. Which languages are popular for object-oriented programming?
Ans:
Languages widely used for object-oriented programming include Java, C++, Python, C#, Ruby and JavaScript. While JavaScript is primarily a scripting language, it supports object-oriented principles like classes, objects and inheritance, making it versatile for OOP concepts.
4. What distinguishes JavaScript's let and var variables?
Ans:
The let keyword has block-level scope, while var has function-level scope. Let cannot be re-declared in the same scope, unlike var. Modern JavaScript prefers let because it reduces bugs and improves code clarity and maintainability.
5. What does NaN mean in JavaScript?
Ans:
NaN stands for “Not-a-Number” and is used when a value cannot be interpreted as a valid number. It commonly appears in invalid mathematical operations like dividing zero by zero. NaN signals an error in numeric computations.
6. What is the difference between passing by value and passing by reference?
Ans:
Passing by value sends a copy of the actual value to a function, leaving the original unchanged, usually for primitive types. Passing by reference sends the address of an object so any changes in the function affect the original object, such as arrays or objects.
7. What is strict mode in JavaScript and why is it used?
Ans:
Stricter error handling and parsing are enforced in strict mode to improve code quality. It prevents undeclared variables, forbids duplicate parameters and throws errors when modifying read-only properties. Using strict mode results in cleaner, safer and more reliable code.
8. How does the this keyword work in JavaScript?
Ans:
In JavaScript, this refers to the object executing the current function. In methods, it points to the owning object. In regular functions, it’s undefined in strict mode or refers to the global object otherwise. Arrow functions inherit this from the surrounding scope.
9. What are JavaScript design patterns?
Ans:
Design patterns are proven solutions for common programming challenges. JavaScript patterns like Module, Singleton, Observer, Factory and Prototype help developers structure code efficiently. They improve readability, maintainability and reuse of code.
10. What is the DOM?
Ans:
A programming interface called the DOM stands for HTML or XML documents as a tree of objects. JavaScript can manipulate the DOM to dynamically update a web page’s structure, content and styling, enabling interactive web applications.