JavaScript - Basic Flashcards
What is JavaScript?
JavaScript is a high-level, interpreted programming language primarily used for developing dynamic web applications. It is a scripting language that allows developers to add interactivity and functionality to web pages. JavaScript can be executed on the client-side, within the user’s web browser, or on the server-side using technologies such as Node.js.
What are the key features of JavaScript?
Dynamic typing
Prototypal inheritance
First-class functions
Event-driven programming
Support for manipulating and traversing the Document Object Model (DOM)
Dynamic typing
JavaScript variables are not bound to specific data types and can hold values of any type.
Prototypal inheritance
Objects in JavaScript can inherit properties and methods from other objects.
First-class functions
Functions in JavaScript are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned as values from functions.
Event-driven programming
JavaScript allows handling events and executing code in response to user actions or system events.
Support for manipulating and traversing the Document Object Model (DOM)
JavaScript provides APIs to interact with HTML and XML documents, allowing dynamic modification of their content and structure.
What are the differences between JavaScript and Java?
Java is a statically-typed language, while JavaScript is dynamically typed.
JavaScript is primarily used for client-side web development, whereas Java is used for a wide range of applications, including web development, server-side development, mobile apps, and enterprise software.
Java code is compiled into bytecode and executed on a virtual machine (JVM), whereas JavaScript code is interpreted and executed by the web browser.
Java has a strong object-oriented programming (OOP) focus, while JavaScript supports both OOP and functional programming paradigms.
Java has a larger standard library compared to JavaScript.
Java is a compiled language, requiring a separate compilation step, while JavaScript is typically interpreted at runtime.
How do you include JavaScript code in an HTML file?
Inline
Internal
External
How do you include JavaScript in HTML inline?
JavaScript code can be directly placed within the
tags within the HTML file.
<script> // JavaScript code goes here </script>
How do you include JavaScript in HTML internally?
JavaScript code can be placed within the
tags in the <head> or <body> section of the HTML file.
How do you include JavaScript in HTML internally?
JavaScript code can be written in a separate .js file and included in the HTML file using the
tag with the src attribute pointing to the JavaScript file.
What are the different data types in JavaScript?
Primitive types
Complex types
Additionally, ES6 introduced the symbol type.
Examples of primitive types?
string, number, boolean, null, and undefined.
Example of complex types?
object (including arrays and functions).
Explain the concept of hoisting in JavaScript.
Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, before the code is executed. This means that you can use variables and call functions before they are declared in the code, without encountering errors. However, only the declarations are hoisted, not the initializations or assignments.
What are the different ways to define variables in JavaScript?
var
let
const
define var
This was the traditional way of declaring variables in JavaScript prior to the introduction of ES6. Variables declared with var are function-scoped or globally scoped.
define let
Introduced in ES6, ‘let’ allows block-scoped variable declarations. Variables declared with let are limited to the block scope in which they are defined.
define const
Also introduced in ES6, ‘const’ is used to declare variables that are block-scoped and whose values cannot be re-assigned once defined.
However, ‘const’ does not make objects immutable; it only prevents re-assignment of the variable itself.
How do you check the data type of a variable in JavaScript?
You can use the typeof operator to check the data type of a variable. It returns a string indicating the type of the operand. For example:
typeof variableName;
This will return a string representing the type of the variableName.
Explain the difference between null and undefined in JavaScript.
null is an assignment value that represents the intentional absence of any object value. It is a primitive value that can be assigned to a variable to indicate the absence of an object.
undefined is a built-in value that represents an uninitialized, missing, or unknown value. It is the default value assigned to variables that have been declared but not initialized or assigned a value.
What are the arithmetic operators in JavaScript?
Addition: +
Subtraction: -
Multiplication: *
Division: /
Remainder (Modulus): %
Increment: ++
Decrement: –
Exponentiation: **