1. What are the differences between JavaScript and Java?
Ans:
Java is a compiled, statically typed, object-oriented programming language mainly used for building large-scale applications and runs on the Java Virtual Machine (JVM). In contrast JavaScript is an interpreted, dynamically typed scripting language primarily designed for web development, running in browsers or Node.js environments. Although their syntax might look somewhat similar, they serve different purposes and operate in different 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 or reference types include Objects, Arrays and Functions, which store collections or executable code.
3. Which are the main languages used in object-oriented programming?
Ans:
Popular object-oriented programming (OOP) languages include Java, C++, Python, C#, Ruby and JavaScript. These languages use classes and objects to organize and structure code effectively.
4. What is the difference between the JavaScript keywords let and var?
Ans:
The keyword let is block-scoped meaning it is only accessible within the nearest set of curly braces, while var is function-scoped. Also, let cannot be re-declared within the same scope, which helps avoid bugs, making it the preferred choice in modern JavaScript.
5. What does the NaN property in JavaScript mean?
Ans:
NaN stands for “Not-a-Number” and represents a value that is not a valid numeric value. It typically results from invalid mathematical operations such as dividing zero by zero.
6. What is the difference between pass by value and pass by reference?
Ans:
Pass by value copies the actual value to a function, so changes inside the function don’t affect the original variable this applies to primitive types. Pass by reference passes the memory address of an object or array, so changes affect the original data outside the function.
7. What is strict mode in JavaScript and what are its characteristics?
Ans:
Strict mode, enabled by adding 'use strict'; at the start of a script or function, enforces stricter parsing and error handling. It prevents the use of undeclared variables, throws errors on assigning to read-only properties, disallows duplicate parameter names and generally helps write cleaner, more secure code.
8. What does the this keyword represent in JavaScript?
Ans:
The object carrying out the current function is referred to using the this keyword. In methods it refers to the object owning the method. In regular functions, this is undefined in strict mode or refers to the global object in non-strict mode. In arrow functions this is lexically bound meaning it inherits from the surrounding scope.
9. What are JavaScript Design Patterns?
Ans:
JavaScript design patterns are reusable solutions for common programming problems, improving code readability and maintainability. Common patterns include the Module, Singleton, Observer, Factory and Prototype patterns, each serving specific architectural purposes.
10. What is DOM, or the Document Object Model?
Ans:
Web documents can be programmed using the DOM representing HTML or XML as a tree structure of objects. JavaScript uses the DOM to dynamically change a page’s content, structure and style by interacting with these objects.