1. How does JavaScript differ from Java?
Ans:
Java is a statically typed, compiled and object-oriented programming language, mainly used for building large-scale enterprise applications. JavaScript, on the other hand, is dynamically typed, interpreted and primarily used as a scripting language for web development. While Java runs on the Java Virtual Machine (JVM), JavaScript executes inside browsers or in server environments like Node.js. Even though their syntax may look alike both serve completely different use cases.
2. What types of data does JavaScript support?
Ans:
JavaScript data types are broadly categorized into primitive and non-primitive types. Primitive types consist of String, Number, Boolean, Null, Undefined, Symbol and BigInt, which hold single values. Non-primitive types, also called reference types, include Objects, Arrays and Functions, which store multiple values or structured data, allowing more flexible programming.
3. Which programming languages use Object-Oriented Programming (OOP)?
Ans:
Many popular languages embrace object-oriented programming. Java, C++, Python, C#, Ruby and JavaScript are among the most widely used. These languages allow developers to work with objects and classes, enabling modular development, code reuse and better maintainability of complex systems.
4. How do let and var differ in JavaScript?
Ans:
Both let and var are used for declaring variables but their scope and behavior differ. The let keyword is block-scoped, meaning the variable is restricted to block in which it is defined and cannot be redeclared within the same scope. In contrast, var is function-scoped and allows redeclaration. Modern best practice favors let, as it reduces common mistakes in coding.
5. What does NaN represent in JavaScript?
Ans:
NaN, short for “Not-a-Number,” indicates that a value is not a valid numeric type. It typically shows up when someone tries to do arithmetic on non-numeric values or divides zero by zero, among other improper mathematical operations. Instead of breaking the program, NaN acts as a signal that an invalid numeric result has occurred.
6. Can you explain the difference between pass by value and pass by reference?
Ans:
When data is passed by value, a copy of the actual value is created so changes made to, it do not affect the original variable. In contrast pass by reference shares actual memory address of the object so modifications directly affect original. Understanding this behavior is essential for writing predictable and efficient programs.
7. What is strict mode in JavaScript and why is it used?
Ans:
Strict mode, enabled by adding "use strict"; at the top of a script or function, applies stricter rules to JavaScript execution. It disallows undeclared variables, prevents accidental creation of global variables, restricts duplicate parameter names and ensures certain assignments throw errors. This mode enforces better coding practices and reduces potential bugs.
8. What does the keyword "this" signify in JavaScript?
Ans:
The object that possesses the current execution context is identified by the "this" keyword in JavaScript. In arrow functions, it inherits its value from the surrounding lexical scope, but in ordinary functions, it usually points to the global object (or undefined in strict mode). This helps functions and objects determine their context during execution.
9. What are some common design patterns in JavaScript?
Ans:
Design patterns provide tried-and-tested solutions to recurring programming problems. In JavaScript, widely used patterns include Module, Singleton, Observer, Factory and Prototype. By applying these patterns, developers can structure their code in a way that is more maintainable, scalable and easier to understand.
10. What is meant by the DOM in JavaScript?
Ans:
The Document Object Model (DOM) is a programming interface that represents an HTML or XML document as a hierarchical tree of objects. It allows developers to dynamically update, modify and manipulate website content, structure and styling at runtime.