JavaScript Fundamentals Quiz
44 Questions
0 Views

JavaScript Fundamentals Quiz

Created by
@WellMadeHolly

Podcast Beta

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the purpose of using backticks in JavaScript strings?

  • To create static strings without dynamic inputs
  • To reduce the code complexity and allow dynamic inputs (correct)
  • To enforce strict string formatting rules
  • To prevent variable interpolation within the string
  • Which statement correctly describes an anonymous function in JavaScript?

  • An anonymous function can only be invoked where it is defined. (correct)
  • An anonymous function can be called multiple times without storing it in a variable.
  • An anonymous function must have a name for reference.
  • An anonymous function cannot access its arguments once defined.
  • Which of the following demonstrates the correct syntax for an anonymous arrow function?

  • var greet = function() {}
  • (() => { console.log('Hello'); })() (correct)
  • const greet = function => {}
  • function() { console.log('Hello'); }
  • How many types of loops are primarily used in JavaScript?

    <p>Four: for loops, for-in loops, while loops, and do...while loops</p> Signup and view all the answers

    What is the result of the following code: document.getElementById('demo').innerHTML = 5 + 6?

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

    Which method can only be used while a document is being parsed?

    <p>document.write()</p> Signup and view all the answers

    What output will the following code produce: alert(sum); where sum is the result of x + y and x = 10, y = 20?

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

    What type of data is represented by the value: 'Hello World!'?

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

    Which of the following methods is primarily used for debugging purposes in JavaScript?

    <p>console.log()</p> Signup and view all the answers

    What is the correct way to initialize a string variable in JavaScript?

    <p>var name = 'John';</p> Signup and view all the answers

    What type of data does the value null represent in JavaScript?

    <p>The absence of a value</p> Signup and view all the answers

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

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

    What is the correct way to include an external JavaScript file in an HTML document?

    &lt;script src='file.js'>&lt;/script> Signup and view all the answers

    Which of the following statements about client-side and server-side scripting is true?

    <p>Client-side scripts are interpreted by the web browser.</p> Signup and view all the answers

    In JavaScript, which of the following is true regarding case sensitivity?

    <p>Function names must always be typed with consistent capitalization.</p> Signup and view all the answers

    What is the output of the following JavaScript code: var x = 5; var y = 10; var sum = x + y; document.write(sum);?

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

    How can JavaScript be used in relation to user input in forms?

    <p>To check for invalid data entered in form fields.</p> Signup and view all the answers

    What is the purpose of the alert function in JavaScript?

    <p>To display a pop-up message to the user.</p> Signup and view all the answers

    Which of the following JavaScript code snippets correctly defines a function?

    <p>function sayHello() { alert('Hello!'); }</p> Signup and view all the answers

    What type of output can be expected from the following code: document.write(greet); when greet is set to 'Hello World!'?

    <p>Outputs 'Hello World!' on the web page.</p> Signup and view all the answers

    What is the primary function used to convert a JSON string into a JavaScript object?

    <p>JSON.parse()</p> Signup and view all the answers

    Which of the following statements about JSON and functions is true?

    <p>Functions must be converted into strings to be represented in JSON.</p> Signup and view all the answers

    What will happen if a JavaScript object containing a function is converted to JSON using JSON.stringify()?

    <p>The function will be ignored and not included in the JSON string.</p> Signup and view all the answers

    Given the JavaScript code, what is the result of executing document.getElementById('demo').innerHTML = obj.employees.firstName + ' ' + obj.employees.lastName;?

    <p>It will throw an error because 'employees' is an array.</p> Signup and view all the answers

    When receiving data from a web server, how should the string data be processed in JavaScript?

    <p>It must be parsed using JSON.parse() to be usable.</p> Signup and view all the answers

    In the provided JavaScript code, what does the variable 'obj' represent after executing var obj = JSON.parse(text);?

    <p>A JavaScript object constructed from the parsed JSON data.</p> Signup and view all the answers

    What is the correct way to parse a variable txt containing a JSON string?

    <p>var obj = JSON.parse(txt);</p> Signup and view all the answers

    Which of the following JSON strings is correctly formatted?

    <p>{ &quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30 }</p> Signup and view all the answers

    What is the purpose of the 'else if' statement in an if...else if...else structure?

    <p>To create an alternative condition if the first condition is false</p> Signup and view all the answers

    In a switch...case statement, what happens if none of the cases match the value of the variable?

    <p>The block of code under 'default' will be executed</p> Signup and view all the answers

    Which of the following statements correctly depicts a JavaScript block statement?

    <p>if (x == 5) { console.log(x); }</p> Signup and view all the answers

    What will be the output of the following code? var a = 10; { var a = 20; } console.log(a);

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

    What is the role of the 'break' statement in a switch...case statement?

    <p>To stop execution of the switch statement and exit</p> Signup and view all the answers

    What happens when you declare a variable using 'var' inside a block statement?

    <p>The variable is accessible outside the block</p> Signup and view all the answers

    Given the JavaScript code, what will be alerted if today is Tuesday (dayOfWeek == 2)? var now = new Date(); var dayOfWeek = now.getDay(); if(dayOfWeek == 5) { alert('Have a nice weekend!'); } else if(dayOfWeek == 0) { alert('Have a nice Sunday!'); } else { alert('Have a nice day!'); }

    <p>Have a nice day!</p> Signup and view all the answers

    What keyword is used to execute code when a switch case matches?

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

    What is the main benefit of using string interpolation in JavaScript?

    <p>It simplifies the code by reducing the number of console statements.</p> Signup and view all the answers

    How are anonymous functions typically accessed in JavaScript?

    <p>By the variable they are assigned to.</p> Signup and view all the answers

    Which of the following correctly illustrates the structure of a for loop in JavaScript?

    <p>for (let i = 0; i &lt; 10; i++) { console.log(i); }</p> Signup and view all the answers

    What is a limitation of using anonymous functions in JavaScript?

    <p>They cannot be reused once defined.</p> Signup and view all the answers

    Which of the following statements about the do...while loop is correct?

    <p>It runs the block of code at least once before checking the condition.</p> Signup and view all the answers

    What is true about the use of the function keyword in an anonymous function?

    <p>The function keyword is used without the function name.</p> Signup and view all the answers

    Why might one prefer to use an anonymous function over a named function in JavaScript?

    <p>To avoid polluting the global scope.</p> Signup and view all the answers

    Which type of loop checks its condition after executing its block of code at least once?

    <p>do...while loop</p> Signup and view all the answers

    Study Notes

    Embedding JavaScript

    • You can add JavaScript code directly within HTML using <script> tags.
    • JavaScript code can be written in a separate .js file and included using the src attribute in the <script> tag.

    Client-Side vs. Server-Side Scripting

    • Client-side scripting languages (JavaScript, VBScript) execute in the user's web browser.
    • Server-side scripting languages (PHP, ASP, Java, Python, Ruby) execute on the web server and send HTML to the browser.
    • Server-side scripts are slower due to remote processing.

    JavaScript Syntax

    • JavaScript programs are made of JavaScript statements.
    • JavaScript is case-sensitive.
    • Use document.getElementById(id) to access HTML elements, innerHTML to modify content.

    JavaScript Output Methods

    • document.write( ) writes content to the current document while it's being parsed.
    • alert ( ) displays a pop-up dialog box to the user.
    • console.log() displays data in the browser's developer console for debugging purposes.

    JavaScript Data Types

    • String: Used to represent text data enclosed in single or double quotes.
    • Number: Used for numeric values.
    • Boolean: Represents truth (true) or falsity (false).
    • Array: A collection of data items stored in a specific order.
    • Undefined: Indicates a variable has been declared but not assigned a value.
    • Null: Represents the intentional absence of a value.
    • Object: A collection of key-value pairs, used to represent complex data structures.
    • Function: A block of code that can be executed on demand.

    String Interpolation

    • Use backticks ( ) to create a string where expressions within curly braces ({}) are evaluated and inserted into the string.

    Anonymous Functions

    • Functions without a name.
    • Declared using the function keyword.
    • Accessible only through a variable it's stored in.
    • Can be defined using arrow function syntax: ( () => { // Function Body… } )();.

    JavaScript Loops

    • Loops allow you to repeat code efficiently.
    • Types of loops:
      • for loop: Repeats a block of code as long as a condition is met.
      • for...in loop: Iterates through the properties of an object.
      • while loop: Repeats a block of code as long as a condition is true.
      • do…while loop: Executes a block of code at least once, and then repeats as long as a condition is true.

    JavaScript Conditional Statements

    • if...else if...else Statement: Evaluates conditions in order.
    • switch...case Statement: Tests a variable against multiple values and executes the corresponding block of code.
    • break Statement: Used to exit a loop or switch statement.

    JavaScript Block Statements

    • Block statements group zero or more statements.
    • Used to create scopes for variables declared using var.

    JSON.parse( )

    • Used to convert a JSON string into a JavaScript object.
    • Allows easy access to data within the object.

    JSON.stringify( )

    • Converts a JavaScript object into a JSON string.
    • Removes functions from the object.

    Parsing Functions in JSON

    • Functions are not allowed in JSON.
    • Convert functions to strings before using JSON.stringify().
    • Use eval( ) to convert the string back into a function.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    JavaScript.pdf

    Description

    Test your knowledge on JavaScript fundamentals including embedding code, differences between client-side and server-side scripting, syntax, and output methods. This quiz will cover key concepts and practical applications of JavaScript in web development.

    More Like This

    JavaScript Syntax: .Script Elements
    18 questions
    JavaScript Syntax and Type Conversion Quiz
    12 questions
    JavaScript Rest Parameter Syntax
    10 questions
    Use Quizgecko on...
    Browser
    Browser