1. What are the differences between JavaScript and Java?
Ans:
Java is a compiled, statically typed, object-oriented programming language mainly used for building full-scale applications. JavaScript, on the other hand, is an interpreted, scripting language with dynamic typing that is mostly used for web development. The Java Virtual Machine (JVM) is used to run Java while JavaScript runs in the browser or in Node.js. Although their syntax may appear similar, they serve different purposes and belong to distinct programming ecosystems.
2. What kinds of data types exist in JavaScript?
Ans:
JavaScript has both primitive and non-primitive data types. Primitive types include String, Number, Boolean, Null, Undefined, BigInt and Symbol. Non-primitive types, also called reference types, include Objects, Arrays and Functions. These types allow developers to work with simple values as well as complex data structures.
3. Which are the main languages used in object-oriented programming?
Ans:
Several programming languages support object-oriented programming concepts. The main ones include Java, C++, Python, C#, Ruby and JavaScript. JavaScript supports OOP principles, even though it is mostly utilized as a web application scripting language.
4. What are the differences between the keywords let and var in JavaScript?
Ans:
The keyword let is block-scoped, whereas var is function-scoped. Let cannot be re-declared in the same scope, while var can. Modern JavaScript prefers let because it reduces errors and improves the predictability and maintainability of programs.
5. What is the NaN property in JavaScript?
Ans:
NaN stands for “Not-a-Number” and represents a value that is not a valid number. It usually occurs when performing invalid or mathematical procedures that are not defined, such dividing zero by zero. NaN helps signal errors in calculations and data handling.
6. What is the difference between pass by value and pass by reference?
Ans:
In pass by value, a copy of the actual value is passed to functions, so changes do not affect the original data. This usually applies to primitive types. In pass by reference, the function receives a reference to the initial item, so any modifications directly affect the original data. This applies to objects and arrays.
7. What is strict mode in JavaScript and what are its characteristics?
Ans:
Strict mode, enabled by writing 'use strict', enforces stricter rules in JavaScript to improve code quality. It prevents the use of undeclared variables, throws errors when assigning to read-only properties, disallows duplicate parameter names and generally encourages cleaner and more secure coding practices.
8. What does the this keyword mean in JavaScript?
Ans:
The keyword refers to the object is executing the current function. In methods, it points to the object the method belongs to. In regular functions, it is undefined in strict mode or refers to the global object in non-strict mode. In arrow functions, this is lexically bound and inherits from the parent scope.
9. What are JavaScript design patterns?
Ans:
Design patterns in JavaScript are reusable solutions to common software design problems. Common patterns include Module, Singleton, Observer, Factory and Prototype. They help developers write structured, readable and maintainable code.
10. What is the DOM?
Ans:
The Document Object Model (DOM) allows developers to interact with web content programmatically. JavaScript represents HTML and XML documents as a tree of objects, enabling dynamic updates to the content, design and organization of a webpage.