1. What are the differences between JavaScript and Java?
Ans:
Java and JavaScript are completely different languages despite the similarity in their names. Java is a statically typed, compiled, object-oriented language used for creating large-scale applications and runs on the Java Virtual Machine (JVM). On the other hand, JavaScript is a dynamically typed, interpreted scripting language mainly used for web development and runs in browsers or environments like Node.js. While their syntax may seem similar, they are made for diverse uses and function in different settings.
2. What kinds of data types exist in JavaScript?
Ans:
JavaScript supports two main types of data: primitive and non-primitive. Primitive data types include String, Number, Boolean, Null, Undefined, BigInt and Symbol, which hold simple values. Non-primitive types, also known as reference types, include Object, Array and Function, which are used to store complex data structures or executable code.
3. Which are the main languages used in object-oriented programming?
Ans:
Several of the most widely used languages for object-oriented programming include Java, C++, Python, C#, Ruby and JavaScript. These languages use concepts like classes and objects to organize code, making it more reusable, modular and easier to maintain in large software systems.
4. What is the difference between the JavaScript keywords let and var?
Ans:
In JavaScript, let and var are both used to declare variables but they behave differently. let is block-scoped, which means it is only accessible within the nearest set of curly braces {}. In contrast, var is function scoped and can lead to unexpected behavior. Also, let cannot be redeclared in the same scope, making it a safer and more preferred choice in modern coding practices.
5. What does the NaN property in JavaScript mean?
Ans:
NaN stands for "Not-a-Number" and is a special value that indicates a failed or invalid number operation. For example, dividing zero by zero or trying to convert a non-numeric string into a number will result in NaN. It is the type of Number but does not equal itself.
6. What is the difference between pass by value and pass by reference?
Ans:
In JavaScript primitive data types like numbers and strings are passed by value, meaning a copy of the actual value is passed into functions. The original variable is unaffected by any modifications made inside the function. On the other hand, objects and arrays are passed by reference, meaning the function gets access to the original data's memory location, so changes inside the function reflect outside as well.
7. What is strict mode in JavaScript and what are its characteristics?
Ans:
Strict mode in JavaScript is the way to make your code cleaner and more secure. It is activated by placing 'use strict'; at the top of script or function. This mode helps catch common coding mistakes by preventing the use of undeclared variables, disallowing duplicate parameter names and blocking the assignment to read-only properties. It enforces better coding practices and helps avoid errors that might otherwise go unnoticed.
8. What does the this keyword represent in JavaScript?
Ans:
The object performing the function is indicated by this word. This is a reference to the object to which the method belongs in object methods. The value of this in regular functions is dependent on how the function is called; in strict mode, it is undefined, while in non-strict mode, it refers to the global object. This inherits its value from the surrounding scope in arrow functions rather than binding itself.
9. What are JavaScript Design Patterns?
Ans:
JavaScript design patterns are standard solutions to common problems in software design. They help improve code quality, maintainability and scalability. Examples include the Module pattern, Singleton pattern, Observer pattern, Factory pattern and Prototype pattern.
10. What is DOM or the Document Object Model?
Ans:
The Document Object Model (DOM) is a programming interface that represents a web page's content as a structured tree of objects. JavaScript interacts with the DOM to dynamically change elements, update styles or respond to user actions. It enables programmers to create interactive web pages using modifying HTML and CSS through code.