1. What are the differences between JavaScript and Java?
Ans:
Java and JavaScript are distinct programming languages serving different purposes. Java is a compiled statically typed object oriented language primarily used for building large-scale applications. In contrast JavaScript is an interpreted, dynamically typed scripting language mostly used for web development. While Java runs on the Java Virtual Machine (JVM), JavaScript typically runs in web browsers or on Node.js. Although their syntax may appear similar at first glance, their applications, ecosystems and execution environments are completely different.
2. What are the data types in JavaScript?
Ans:
JavaScript has both primitive and non-primitive data types. Primitive types include String, Number, Boolean, Null, Undefined, BigInt and Symbol, which store simple, immutable values. Non-primitive types, also called reference types, include Object, Array and Function, which can hold multiple values and are mutable. Understanding these types helps in managing data efficiently and avoiding unexpected behavior in applications.
3. Which programming languages support object-oriented programming?
Ans:
Several popular languages implement object-oriented programming concepts such as classes, inheritance and encapsulation. Java, C++, Python, C#, Ruby and JavaScript all support OOP principles, though their syntax and features may differ. Using these languages allows developers to design reusable, modular and maintainable code for complex applications.
4. What is the difference between the JavaScript keywords let and var?
Ans:
In JavaScript, let and var are used to declare variables but behave differently. Let-declared variables cannot be re-declared since they are block scoped in the same scope, making safer and less prone to bugs. Modern JavaScript best practices recommend using let to improve code clarity and reliability.
5. What does NaN mean in JavaScript?
Ans:
NaN stands for Not-a-Number and represents a value is not a valid number. It usually occurs when a mathematical operation fails or is undefined, such as dividing zero by zero. NaN helps developers identify invalid calculations and handle errors in numerical operations effectively.
6. What is the difference between passing by value and passing by reference?
Ans:
Passing by value means that a copy of the actual value is passed to a function, so any changes inside the function do not affect the original variable. This usually applies to primitive types. Passing by reference, on the other hand, sends the reference or address of the original object, so any modifications inside the function directly affect the original object. This concept is crucial for managing data and memory efficiently in JavaScript.
7. What is strict mode in JavaScript and what are its features?
Ans:
Strict mode allows for more stringent enforcement parsing and error handling in JavaScript 'use strict' is added at the start of a function or script. Its main features include preventing the use of undeclared variables, throwing errors when assigning to read-only properties, disallowing duplicate parameter names and promoting cleaner and more secure code. Strict mode helps developers catch common mistakes and write more reliable programs.
8. What does the “this” keyword mean in JavaScript?
Ans:
The this keyword refers to the object is currently executing a function. In methods, this points to the object the method belongs to. In regular functions, this is undefined in strict mode or points to the global object in non-strict mode. In arrow functions, this is lexically bound, meaning it inherits the value of this from the surrounding scope. Understanding this is important for managing context and object behavior.
9. What are JavaScript design patterns?
Ans:
Design patterns are reusable solutions to common issues in software design. In JavaScript, popular patterns include Module, Singleton, Observer, Factory and Prototype. These patterns help structure code in a way that improves readability, maintainability and scalability. Applying design patterns allows developers to create well-organized and efficient applications.
10. What is the DOM?
Ans:
A programming interface for web documents is called the DOM that represents HTML and XML content as a tree of objects. JavaScript can use the DOM to access, modify and update the structure, style and content of web pages dynamically. This allows developers to create interactive websites that respond to user actions and change content in real time.