1. What makes JavaScript different from Java?
Ans:
Java is an object-oriented, statically typed, compiled programming language that is mostly used to create large-scale applications. In contrast, JavaScript is a dynamically typed, interpreted primarily used scripting language for web development. JavaScript runs in server contexts like Node.js or web browsers, while Java is executed on the Java Virtual Machine (JVM). While their syntax may appear similar they serve different purposes and belong to distinct ecosystems.
2. What are the data types in JavaScript?
Ans:
There are two primary data type groups in JavaScript. Primitive types include String, Number, Boolean, Null, Undefined, BigInt and Symbol, which store single values. Non-primitive or reference types include Objects, Arrays and Functions, which can store multiple values or complex structures and allow more flexible data manipulation.
3. Which languages support object-oriented programming (OOP)?
Ans:
Several programming languages support object-oriented programming. Popular ones include Java, C++, Python, C#, Ruby and JavaScript. These languages allow developers to organize code using objects and classes, promoting code reuse, modularity and easier maintenance of software systems.
4. How are let and var different in JavaScript?
Ans:
The keywords let and var are used to declare variables, but they behave differently. let is block-scoped, meaning it is limited to the block in which it is defined and cannot be redeclared within the same scope. var is function-scoped and can be redeclared in the same scope. let is preferred in modern JavaScript as it reduces common coding errors.
5. What does NaN mean in JavaScript?
Ans:
NaN stands for "Not-a-Number" and represents a value that is not a valid number. It usually occurs when mathematical operations are undefined or invalid, such as dividing zero by zero. NaN helps indicate errors in numerical computations without crashing the program.
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 sent and changes to it do not affect the original data. Passing by reference sends a reference to the original object, so any modifications affect the original data. Understanding this distinction is important for managing how changes propagate in programs.
7. What is strict mode in JavaScript and what are its characteristics?
Ans:
Strict mode, activated using 'use strict';, enforces stricter parsing and error handling in JavaScript. It prevents the use of undeclared variables, ensures assignments to read only properties throw errors, disallows duplicate parameter names and generally encourages cleaner, safer and more predictable code.
8. What does the "this" keyword mean in JavaScript?
Ans:
The "this" keyword refers to object that is executing the current function. In regular functions, this can point to global object or be undefined in strict mode, while in arrow functions, it inherits its value from the surrounding scope. It helps determine context within functions and objects.
9. What are JavaScript design patterns?
Ans:
Design patterns are reusable fixes for typical issues in software. In JavaScript, frequently used patterns include Module, Singleton, Observer, Factory and Prototype patterns. These patterns help structure code effectively, making it more readable, maintainable and scalable.
10. What is the DOM in JavaScript?
Ans:
The Document Object Model (DOM) is programming interface that represents HTML and XML documents as a structured tree of objects.Changes to text, look and layout can be made during runtime due to its dynamic interaction with web site content, style and structure.