JavaScript Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which of the following best describes the scope of a variable declared with var inside a function?

  • Block-scoped
  • Function-scoped (correct)
  • Globally-scoped if declared within a block
  • Lexically scoped

Which of the following statements about let is true?

  • It can be re-assigned and is block-scoped. (correct)
  • It must be initialized at the time of declaration and is block-scoped.
  • It can be re-assigned and is function-scoped.
  • It cannot be re-assigned and is block-scoped.

Which statement about const is correct?

  • Variables declared with const must be initialized and cannot be re-assigned. (correct)
  • Variables declared with const can be re-assigned and are block-scoped.
  • Variables declared with const cannot be re-assigned but may be declared without initialization.
  • Variables declared with const are function-scoped.

What does hoisting mean in JavaScript?

<p>Variables declared with var are moved to the top of their scope and initialized with undefined. (A)</p> Signup and view all the answers

What happens if you try to access a let or const variable before its declaration?

<p>It throws a ReferenceError. (B)</p> Signup and view all the answers

Which of the following is a correct rule for naming variables in JavaScript?

<p>Variable names can contain letters, digits, underscores, and dollar signs, but cannot start with a digit. (C)</p> Signup and view all the answers

Which of the following is a primitive data type in JavaScript?

<p>String (C)</p> Signup and view all the answers

Which statement correctly describes the BigInt data type in JavaScript?

<p>BigInt is used to represent arbitrarily large integers, but it cannot be directly mixed with Number types without explicit conversion. (C)</p> Signup and view all the answers

What value does a variable declared with let hold if it is declared but not explicitly initialized (after its declaration line)?

<p>undefined (A)</p> Signup and view all the answers

Which of the following is NOT a valid JavaScript variable name?

<p>2myVar (D)</p> Signup and view all the answers

Which of the following statements correctly describes the behavior of the var keyword?

<p>Variables declared with var are hoisted and initialized with undefined. (B)</p> Signup and view all the answers

Which data type in JavaScript represents the intentional absence of any object value?

<p>null (B)</p> Signup and view all the answers

Which statement best describes the difference between null and undefined in JavaScript?

<p>null represents the absence of any object value, while undefined means a variable has been declared but not assigned a value. (A)</p> Signup and view all the answers

What is the output of the following code?

console.log(a); var a = 5;

<p>undefined (B)</p> Signup and view all the answers

What is the output of the following code?

console.log(b); let b = 10;

<p>ReferenceError (C)</p> Signup and view all the answers

Which statement about hoisting and variable declarations is correct?

<p>All variable declarations are hoisted, but let and const are not initialized until their declaration is evaluated. (C)</p> Signup and view all the answers

Which of the following operations would cause a TypeError due to mixing incompatible types?

<p>5n + 10 (A)</p> Signup and view all the answers

What is the primary purpose of using the const keyword in JavaScript?

<p>To declare a variable that cannot be re-assigned after its initial value is set. (A)</p> Signup and view all the answers

Which of the following is a correct example of a template literal in JavaScript?

<p>(backticks) Hello, ${name}! (backticks) (C)</p> Signup and view all the answers

Which of the following statements about type coercion in JavaScript is true?

<p>JavaScript automatically converts types during operations such as addition, sometimes leading to unexpected results. (B)</p> Signup and view all the answers

What is the output of the following code?

console.log(typeof null);

<p>&quot;object&quot; (B)</p> Signup and view all the answers

What does the term "temporal dead zone" refer to in JavaScript?

<p>The period between entering a block and the execution of a let or const declaration, during which accessing the variable causes a ReferenceError. (B)</p> Signup and view all the answers

Which operator should you use to ensure that both the value and type are compared in JavaScript?

<p>=== (B)</p> Signup and view all the answers

Which of the following is considered a non-primitive (object) in JavaScript?

<p>[1, 2, 3] (D)</p> Signup and view all the answers

What happens if you try to re-declare a variable using let in the same block?

<p>A SyntaxError is thrown. (C)</p> Signup and view all the answers

What is the output of the following code?

console.log("5" + 2);

<p>&quot;52&quot; (B)</p> Signup and view all the answers

What is the output of the following code?

console.log("5" - 2);

<p>3 (B)</p> Signup and view all the answers

What is the output of the following code?

console.log(0 == false);

<p>true (A)</p> Signup and view all the answers

What is the output of the following code?

console.log(0 === false);

<p>false (B)</p> Signup and view all the answers

Which expression correctly ensures that addition happens before multiplication?

<p>(2 + 3) * 4 (B)</p> Signup and view all the answers

What is the primary purpose of using parentheses when calling a function in JavaScript?

<p>To enclose the function arguments (B)</p> Signup and view all the answers

Which of the following correctly uses parentheses to wrap a condition in an if statement?

<p>if (x &gt; 10) { ... } (C)</p> Signup and view all the answers

Which construct creates a new variable scope in JavaScript when used with let or const?

<p>Curly Braces { } used as a block (B)</p> Signup and view all the answers

How can you distinguish between an object literal and a code block when using curly braces?

<p>A code block is used in control structures, while an object literal appears in expressions or assignments. (B)</p> Signup and view all the answers

What does the following code do?

let numbers = [1, 2, 3, 4];

<p>Creates an array containing the values 1, 2, 3, and 4 (C)</p> Signup and view all the answers

How do you access the third element in the array let items = ['a', 'b', 'c', 'd'];?

<p>items[2] (C)</p> Signup and view all the answers

Which of the following is a limitation of using parentheses ( ) in JavaScript?

<p>They do not create a new variable scope. (C)</p> Signup and view all the answers

When you see the following syntax, what is being created?

let userArray = [{ name: "John", age: 30 }];

<p>An array containing a single object (A)</p> Signup and view all the answers

What is the role of curly braces in the following if statement?

if (x > 0) { let message = "Positive"; console.log(message); }

<p>To group multiple statements into a code block and create a new scope (B)</p> Signup and view all the answers

Which of the following is an example of implicit type coercion in JavaScript?

<p>&quot;5&quot; + 3 (B)</p> Signup and view all the answers

Implicit type coercion occurs automatically without explicit instructions from the programmer.

<p>True (A)</p> Signup and view all the answers

In JavaScript, the === operator checks for both _____ and _____, preventing implicit coercion.

<p>value, type</p> Signup and view all the answers

What happens when you run the following JavaScript code?

console.log("6" - 2);

<p>4 (B)</p> Signup and view all the answers

Strongly typed languages like Java and C++ allow implicit coercion between numbers and strings.

<p>False (B)</p> Signup and view all the answers

In JavaScript, using the + operator between a number and a string results in _____, while using -, *, or / usually results in _____.

<p>string concatenation, number conversion</p> Signup and view all the answers

Which of the following operations will result in implicit type coercion in JavaScript?

<p>console.log(&quot;5&quot; * 2); (A), console.log(10 + true); (B)</p> Signup and view all the answers

In JavaScript, Number("123") is an example of implicit type coercion.

<p>False (B)</p> Signup and view all the answers

The process of manually converting a value from one type to another is called _____.

<p>explicit typecasting</p> Signup and view all the answers

What will the following JavaScript code output?

console.log(10 + true);

<p>11 (A)</p> Signup and view all the answers

Primitive types are stored by reference in memory.

<p>False (B)</p> Signup and view all the answers

Which of the following are primitive types in JavaScript?

<p>String (A), Number (C)</p> Signup and view all the answers

Primitive types are ________, meaning they cannot be changed once created.

<p>immutable</p> Signup and view all the answers

Non-primitive types are mutable, meaning their values can be modified without creating a new memory allocation.

<p>True (A)</p> Signup and view all the answers

Which statement correctly describes primitive vs. non-primitive types?

<p>Primitive types behave like a built-in memory card (read-only). (A), Non-primitive types behave like a temporary memory card (editable). (B)</p> Signup and view all the answers

Data structures provide a way to organize and store data efficiently so that it can be accessed, modified, and managed effectively.

<p>True (A)</p> Signup and view all the answers

Data structures provide a way to organize and store data so that it can be ________, modified, and managed effectively.

<p>accessed</p> Signup and view all the answers

Which of the following best describes one major benefit of choosing an appropriate data structure?

<p>It improves the speed and efficiency of operations like search, insertion, and deletion. (B)</p> Signup and view all the answers

The choice of a data structure does not affect the speed of operations such as search, insertion, and deletion.

<p>False (B)</p> Signup and view all the answers

Which of the following are the three main types of data structures mentioned in the review?

<p>Linear Data Structures, Non-Linear Data Structures, Hash-Based Structures (A)</p> Signup and view all the answers

Linear data structures are also known as ________ data structures because they are organized sequentially.

<p>sequential</p> Signup and view all the answers

Which of the following is an example of a non-linear data structure?

<p>Tree (C)</p> Signup and view all the answers

Hash-based structures typically store data as key-value pairs and are designed for fast lookup operations.

<p>True (A)</p> Signup and view all the answers

Key operations on data structures include insertion, deletion, access/traversal, and ________.

<p>searching</p> Signup and view all the answers

Data structures determine how data is stored, accessed, and manipulated.

<p>True (A)</p> Signup and view all the answers

Which type of collection is organized by indexes?

<p>Indexed Collections (A)</p> Signup and view all the answers

Structured data is like having a well-organized filing system where every piece of information is stored in a specific ________.

<p>place</p> Signup and view all the answers

Which of the following is a common use for JSON (JavaScript Object Notation)?

<p>Representing data in APIs and configurations (A)</p> Signup and view all the answers

In JSON, keys must always be enclosed in quotes, whereas in JavaScript object literals, quotes are not required for keys.

<p>True (A)</p> Signup and view all the answers

Which of the following is NOT a valid JSON data type?

<p>Function (D)</p> Signup and view all the answers

The equality operator == in JavaScript performs ________ conversion before comparing two values.

<p>type</p> Signup and view all the answers

The strict equality operator === compares both the value and the data type of two operands.

<p>True (A)</p> Signup and view all the answers

The strict equality operator === compares both the ________ and the ________ of two operands.

<p>value, type</p> Signup and view all the answers

Which of the following best describes the overall role of data structures?

<p>All of the above. (D)</p> Signup and view all the answers

What is the main advantage of using the strict equality operator (===) over the loose equality operator (==) in JavaScript?

<p>It ensures that both the value and the data type are exactly the same. (C)</p> Signup and view all the answers

Core to looping is the idea of _____. This means you want to perform the same set of actions many times.

<p>repetition</p> Signup and view all the answers

Is the process of going through items one by one (for example, each element in an array).

<p>Iteration</p> Signup and view all the answers

Conditional termination means that a loop stops executing when a specified condition changes (or when a break is encountered).

<p>True (A)</p> Signup and view all the answers

When should you use a for loop?

<p>When you know in advance how many iterations are required or need an index counter (C)</p> Signup and view all the answers

Which loop type is best when the number of iterations is unpredictable and depends on a condition?

<p>While loop (B)</p> Signup and view all the answers

Which loop guarantees that the code block will run at least once, even if the condition is false from the start?

<p>Do...while loop (C)</p> Signup and view all the answers

When iterating over the keys (property names) of an object in JavaScript, which loop should you use

<p>For...in loop (C)</p> Signup and view all the answers

If you want to iterate directly over the values of an iterable object (like an array or string), which loop is most suitable?

<p>For...of loop (D)</p> Signup and view all the answers

A for loop is best used when you know the number of iterations because it handles initialization, condition, and __________ all together.

<p>update</p> Signup and view all the answers

A while loop may run zero times if the condition is false initially.

<p>True (A)</p> Signup and view all the answers

A do...while loop always executes its code block at least once before checking the condition.

<p>True (A)</p> Signup and view all the answers

The loop stops executing when a specified condition changes or a break is encountered; this is called __________ termination.

<p>conditional</p> Signup and view all the answers

Which statement best distinguishes sequences, selections, and loops?

<p>Sequences execute in a specific order, selections choose between different paths, and loops repeat tasks. (A)</p> Signup and view all the answers

A ______ statement is used to immediately exit a loop before its condition naturally becomes false.

<p>break</p> Signup and view all the answers

Which loop is best when you know the exact number of iterations you need, such as processing every element in a fixed-size array?

<p>For loop (C)</p> Signup and view all the answers

Which loop should you use when you need to guarantee that a code block runs at least once, even if the condition is initially false?

<p>Do...while loop (C)</p> Signup and view all the answers

The for...in loop is primarily used to iterate over arrays.

<p>False (B)</p> Signup and view all the answers

The for...of loop iterates directly over the values of an iterable object, making it ideal for arrays and strings.

<p>True (A)</p> Signup and view all the answers

A loop stops executing when its condition becomes __________.

<p>false</p> Signup and view all the answers

In the context of looping, the process of going through each item in a set one by one is called:

<p>Iteration (A)</p> Signup and view all the answers

A semicolons is required after a statement in JavaScript.

<p>False (B)</p> Signup and view all the answers

How many statements are present inside the function block?

function genie() { let a, b, c; a = 5; b = 9; c = a + b; }

<p>4 (A)</p> Signup and view all the answers

What will be the alerted result?

let x = 5; x = 7; x = x + x; alert(x);

<p>14 (B)</p> Signup and view all the answers

With the let keyword, you can declare variables with the same name in the same block.

<p>False (B)</p> Signup and view all the answers

The break statement can be used to exit any block of code, including functions, if statements, and loops.

<p>False (B)</p> Signup and view all the answers

What is the primary difference between using return and break inside a loop that is inside a function?

<p>return exits the function entirely, while break only exits the loop. (B)</p> Signup and view all the answers

In JavaScript, the __________ statement is used to immediately exit a loop or switch statement, while the __________ statement is used to exit a function and optionally return a value.

<p>break, return</p> Signup and view all the answers

Using return inside a loop will exit the loop but continue executing any code after the loop in the same function.

<p>False (B)</p> Signup and view all the answers

Which of the following statements is correct about the break statement?

<p>break can only be used within loops and switch statements. (B)</p> Signup and view all the answers

The return statement cannot be used outside of a function.

<p>True (A)</p> Signup and view all the answers

Consider the following code:

function testLoop() { for (let i = 0; i < 5; i++) { if (i === 3) { break; } console.log(i); } console.log("Loop ended"); } testLoop();

<p>0, 1, 2, &quot;Loop ended&quot; (B)</p> Signup and view all the answers

When a return statement is executed inside a function, it immediately terminates the function's execution and optionally returns __________.

<p>value</p> Signup and view all the answers

If you want to exit a loop early without stopping the execution of the rest of the function, you should use:

<p>break (B)</p> Signup and view all the answers

Using break in a switch statement prevents the execution from "falling through" to subsequent cases.

<p>True (A)</p> Signup and view all the answers

Flashcards

Capital of France (example flashcard)

Paris

Study Notes

Glossary


Type Conversion & Coercion

Implicit Conversion (Coercion):

  • Definition: The automatic conversion of values from one data type to another by the compiler or interpreter.
  • Context: Happens without explicit direction from the programmer (e.g., converting a number to a string during concatenation).
  • Key Point: Usually a safe conversion (often converting from a smaller to a larger type) with no data loss.

Explicit Conversion (Casting):

  • Definition: The manual conversion of values from one type to another, as directed by the programmer.
  • Context: Used when converting from a larger type to a smaller type or when implicit coercion isn’t acceptable.
  • Key Point: Can cause data loss; used only when needed.

Conversion vs. Coercion:

  • Conversion: Generally refers to explicit, manual conversion (casting).
  • Coercion: Often used to describe implicit, automatic type conversion.

Core Concepts of Looping

Repetition:

  • Definition: Performing the same set of actions multiple times.
  • Context: The foundational idea behind loops—repeat tasks until a condition is met.

Iteration:

  • Definition: The process of going through items one by one (e.g., elements in an array or properties of an object).
  • Context: Iteration is used to process each element in a collection sequentially.

Conditional Termination:

  • Definition: The loop stops executing when a specified condition changes (or when a break statement is encountered).
  • Context: All loops check a condition to decide whether to continue or stop.

Looping Constructs

For Loop:

  • Definition: A loop that integrates initialization, condition-checking, and updating in one statement.
  • Usage: Use when you know in advance how many iterations are required or when you need an index counter (e.g., iterating over an array).
  • Analogy: Counting items on a checklist when you know the exact number.

While Loop:

  • Definition: A loop that executes its block of code as long as a condition is true.
  • Usage: Use when the number of iterations isn’t known in advance and depends on dynamic conditions.
  • Analogy: Continuing to search until you no longer find what you’re looking for.

Do...While Loop:

  • Definition: A loop that executes its code block at least once before checking the condition.
  • Usage: Use when the code should run at least once (e.g., prompting a user for input) regardless of the initial condition.
  • Key Phrase: "Do the code, then check if we should do it again."

For...in Loop:

  • Definition: A loop used to iterate over the enumerable property keys of an object.
  • Usage: Ideal for when you need to process each key in an object rather than iterating over an array’s values.

For...of Loop:

  • Definition: A loop used to iterate directly over the values of an iterable object (like arrays, strings, etc.).
  • Usage: Provides a cleaner syntax when you want to work directly with the elements (values) rather than their indices.

Break Statement:

  • Definition: A control statement used to exit a loop immediately, regardless of whether its condition is still true.
  • Usage: Use when you want to stop iterating early once a specific condition is met.

Sequences:

  • Definition: A specific, linear order in which statements or tasks are executed.
  • Context: In programming, sequences ensure that code runs in a predetermined, step-by-step order.

Selections:

  • Definition: Conditional constructs (e.g., if/else, switch) that allow the program to choose between multiple paths based on different conditions.
  • Context: Also known as branching, similar to a tree with many branches.

Loops:

  • Definition: Constructs that enable the repetitive execution of a block of code until a condition changes.
  • Context: Combine repetition, iteration, and conditional termination to automate repetitive tasks.

Purpose of Data Structures:

  • Definition: Data structures provide a way to organize and store data so that it can be efficiently accessed, modified, and managed.
  • Key Points:
    • Organization & Storage: They structure data in an orderly fashion.
    • Efficiency: The choice of data structure affects the performance of operations such as searching, insertion, and deletion.

Types of Data Structures:

  • Linear Data Structures: Organized sequentially (e.g., arrays, linked lists).
  • Non-Linear Data Structures: Organized hierarchically or as networks (e.g., trees, graphs).
  • Hash-Based Structures: Use key-value pairs for fast lookups (e.g., hash tables, dictionaries).

Key Operations on Data Structures:

  • Insertion: Adding data to the structure.
  • Deletion: Removing data from the structure.
  • Access/Traversal: Retrieving or navigating through data.
  • Searching: Locating data within the structure.

Indexed Collections vs. Keyed Collections:

  • Indexed Collections: Organized by position (e.g., arrays).
  • Keyed Collections: Organized by keys (e.g., objects, dictionaries).

Structured Data:

  • Definition: Data organized in a systematic way (like a well-organized filing system).
  • Context: Makes data easy to locate and manage.

JSON (JavaScript Object Notation)

Definition:

  • A lightweight data representation format used primarily for APIs and configuration files.

Characteristics:

  • Human-readable and easy to write.
  • Similar to XML or YAML but often preferred for its simplicity.
  • Note on Keys: JSON requires keys to be in quotes, whereas JavaScript objects may omit quotes for keys if they are valid identifiers.

JSON Data Types:

  • Strings, Numbers, Booleans, Null, Arrays, Objects.

Equality Comparison

== (Loose Equality):

  • Definition: An equality operator that performs type conversion (coercion) before comparing two values.
  • Context: Can lead to unexpected results due to automatic conversion.

=== (Strict Equality):

  • Definition: An equality operator that compares both value and data type without performing type conversion.
  • Context: Returns true only if both the value and type are identical.

Additional Concepts

Variable Declaration:

  • Definition: The process of creating a variable to store data.
  • Context: Involves keywords such as var, let, or const in JavaScript.

Hoisting:

  • Definition: A JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during compilation.
  • Context: Affects how and when variables are available in your code.

Temporal Dead Zone (TDZ):

  • Definition: The time span between entering a block and the point where a variable declared with let or const is initialized, during which the variable cannot be accessed.
  • Context: Ensures that variables are not accessed before they are declared and initialized.

Variable Naming Rules:

  • Definition: The conventions and rules for naming variables in a programming language (e.g., must start with a letter, underscore, or dollar sign in JavaScript; case-sensitive; no reserved keywords).
  • Context: Ensures clarity and prevents conflicts with language syntax.

Template Literals:

  • Definition: A way to define strings in JavaScript using backticks (`) that allow for embedded expressions (e.g., ${variable}) and multi-line strings.
  • Context: Provides a more readable and concise method for string interpolation and formatting compared to traditional concatenation.

Data Types:

  • Primitive Types:

    • Definition: Basic data types that are stored by value and are immutable (e.g., Number, String, Boolean, Null, Undefined, Symbol, BigInt).
    • Analogy: Like a built-in memory card that is read-only.
  • Non-Primitive Types:

    • Definition: Complex data structures that are stored by reference and are mutable (e.g., Objects, Arrays, Functions).
    • Analogy: Like a temporary memory card that can be modified.

Type Casting / Conversion:

  • Definition: Changing a variable from one data type to another, either implicitly (coercion) or explicitly (casting).

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

JavaScript Basics Quiz
5 questions

JavaScript Basics Quiz

RomanticRhodolite avatar
RomanticRhodolite
JavaScript Basics Quiz
10 questions

JavaScript Basics Quiz

MagicalBlessing avatar
MagicalBlessing
JavaScript Overview and Basics
9 questions

JavaScript Overview and Basics

UnrestrictedOrangeTree1920 avatar
UnrestrictedOrangeTree1920
Use Quizgecko on...
Browser
Browser