JavaScript Basics Quiz
24 Questions
1 Views

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

What is the purpose of a variable in JavaScript?

  • To format HTML elements
  • To create functions within scripts
  • To store data (correct)
  • To enhance CSS styling
  • Which of the following is an example of an incorrect way to declare a JavaScript variable?

  • let greeting = 'Hello';
  • declare name = 'John'; (correct)
  • var count = 10;
  • const pi = 3.14;
  • Why are semicolons recommended at the end of JavaScript statements?

  • They are optional but help avoid errors when combining multiple statements. (correct)
  • They indicate the end of a block of code.
  • They are mandatory for loop declarations.
  • They are required for function definitions.
  • What is the difference between internal and external JavaScript?

    <p>Internal JavaScript is defined within the HTML code, while external is in separate files.</p> Signup and view all the answers

    Which statement is true regarding the use of JavaScript statements?

    <p>JavaScript statements can consist of values, operators, expressions, keywords, and comments.</p> Signup and view all the answers

    What does the MDN JavaScript reference primarily provide?

    <p>Guidelines and specifications for JavaScript features.</p> Signup and view all the answers

    What does the expression 'const pi = 3.14;' do in JavaScript?

    <p>Declares a variable that cannot be reassigned.</p> Signup and view all the answers

    What is indicated by the term 'developer tools' in browsers?

    <p>Built-in utilities for debugging and testing JavaScript.</p> Signup and view all the answers

    Which of the following statements about JavaScript's execution process is correct?

    <p>JavaScript reads the script and compiles it into machine language.</p> Signup and view all the answers

    Which option correctly describes what JavaScript cannot do in the browser?

    <p>Access disk drives without consent.</p> Signup and view all the answers

    What is a unique feature of JavaScript compared to other programming languages?

    <p>It has full integration with HTML and CSS.</p> Signup and view all the answers

    Which JavaScript feature enables you to store client-side data?

    <p>Local storage</p> Signup and view all the answers

    Which JavaScript engine is used by Chrome and Opera?

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

    Which of the following statements is true about JavaScript arrays?

    <p>JavaScript arrays are objects that can hold elements of any type.</p> Signup and view all the answers

    Which of the following best describes the purpose of JavaScript functions?

    <p>Functions encapsulate reusable code blocks.</p> Signup and view all the answers

    What should be used to declare a variable in JavaScript that can be reassigned?

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

    What will be the output of the following code: console.log(y); given the declaration of var y = 30; is inside a function?

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

    Which statement about the variable let is true?

    <p>Accessing a let variable before its declaration will throw an error.</p> Signup and view all the answers

    What will happen if you try to declare a variable using const without initializing it?

    <p>It will throw a SyntaxError.</p> Signup and view all the answers

    When should you prefer to use const over let?

    <p>When the variable's value is expected to remain constant.</p> Signup and view all the answers

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

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

    Which of the following statements correctly describes var?

    <p>Var can be redeclared in the same scope.</p> Signup and view all the answers

    How is the value of a variable let a = 10; changed to 20?

    <p>By reassigning it.</p> Signup and view all the answers

    What do you get when you declare a variable without assigning any value in JavaScript?

    <p>It holds a value of undefined.</p> Signup and view all the answers

    Study Notes

    JavaScript Basics

    • JavaScript is a scripting language primarily used for web development.
    • It was founded in 1995 by Brendan Eich.
    • JavaScript is an interpreted language, meaning that the code is read and executed directly, without being translated to machine code beforehand.
    • Scripts are plain text.
    • It has no compilation phase.
    • JavaScript code is executed on loading.

    Outline

    • The outline for a JavaScript course covers various topics, including introduction to JavaScript, variables and data types, operators, dialog boxes, conditional and loop statements, functions, arrays, and objects.

    What is JavaScript?

    • JavaScript "brings web pages to life".
    • JavaScript is not Java.
    • JavaScript can run in web browsers, on servers (Node.js), and using JavaScript engines.
    • V8 (Chrome & Opera)
    • SpiderMonkey (Firefox)

    How JavaScript Engines Work

    • The JS Engine's basic function is to: Read, compile, and quickly execute the code.

    What JavaScript Can Do in the Browser

    • JavaScript can manipulate web pages
    • JavaScript interacts with users.
    • JavaScript interacts with web servers.
    • No direct access to CPU, memory, or HDD in the browser to ensure a safe environment.
    • Add HTML, edit content, and change style to customize web pages.
    • Respond to user interactions like clicks, gestures, and key presses.
    • Send network requests to download and upload files.
    • Store client-side data locally.

    What JavaScript Can't Do in the Browser

    • JavaScript cannot directly access or modify a computer's disk.
    • JavaScript cannot recover files from a disk without user permission.
    • JavaScript cannot interact with a camera or microphone without user consent.
    • JavaScript cannot interact with other browser tabs or domains without permission.

    Why is JavaScript Unique?

    • Full integration with HTML/CSS.
    • Supported and enabled by all major browsers.
    • Used to design User Interfaces (UIs).
    • JavaScript helps in designing front-end/back-end web applications.
    • Enables cross-platform app development.

    Manuals & Specifications

    • The ECMA-262 specification is the primary reference material for JavaScript-related concepts, and a new version is published every year.
    • MDN JavaScript reference is a well-regarded manual.
    • Online compatibility charts are helpful to understand which browsers and engines support specific JavaScript features.

    The Developer Console

    • Web browsers include built-in developer tools (DevTools).
    • F12 or Ctrl+Shift+I typically opens the developer tools.

    How to Insert JavaScript?

    • Internal JavaScript code is inserted directly within the HTML page using <script></script> tags within the <head> or <body> section.
    • External JavaScript is included in separate .js files linked to the HTML page Using <script src = "script.js"></script>

    JavaScript Statements

    • JavaScript statements consist of values, operators, expressions, keywords, and comments.
    • Semicolons are usually recommended but not mandatory at the end of statements when writing on different lines
    • Semicolons are required for multiple statements on one single line.

    JS Variables

    • Variables store data.
    • Variables can change values during JavaScript script execution.
    • Valid variable names start with a letter, underscore, or dollar sign and can include letters, numbers, underscores, or dollar signs.
    • JavaScript is case-sensitive, so different variable names are different (e.g., myVariable, MyVariable, myvariable).
    • Reserved keywords (like let, const, var, if, or for) cannot be used as variable names.
    • Meaningful names make code easier to understand and maintain.
    • Camel case is a naming convention. It starts with a lowercase letter, with subsequent words capitalized.
    • Example: myVariableName

    Variable Declaration

    • let variables are block-scoped.
    • let variables cannot be redeclared.
    • let variables are hoisted, but not initialized.
    • var variables are function-scoped.
    • var variables can be redeclared.
    • var variables are hoisted and initialized with undefined.
    • const variables are block-scoped.
    • const variables cannot be reassigned.
    • const variables must be initialized.
    • Use const by default, and let if reassignment is necessary.

    JS Data Types

    • Primitive Types: number, string, boolean, undefined, null
    • Composite Types: object, array, function

    JS Operators

    • Arithmetic Operators: +, -, *, /, %, ++, --
    • Assignment Operators: =, +=, -=, *=, /=, %=
    • Comparison Operators: ==, ===, !=, !==, >, <, >=, <=
    • Logical Operators: &&, ||, !

    Dialog Boxes

    • Alert Box: Displays a message to the user and waits for acknowledgment.
    • Confirm Box: Asks the user for confirmation with "OK" and "Cancel" options.
    • Prompt Box: Prompts the user to enter input and returns the entered value.

    Conditional and Loop Statements

    • Conditional Statements: Used for decision-making in programming (if, else if, else, switch).
    • Loop Statements: Repeat a block of code until a condition is met (for, while, do-while).

    JS Functions

    • Functions: Reusable blocks of code used for specific tasks and calculations.
    • Parameters: Variables listed in the function's declaration.
    • Arguments: Values passed to a function when calling it.
    • Return Statement: Returns a value from a function and stops its execution.

    JS Arrays

    • Arrays: Data structures used to store multiple values in a single variable.
    • Indexed: Elements are accessed using zero-based indexing.
    • Methods: push(), pop(), shift(), unshift(), slice(), splice(), forEach() are common array methods.

    JS Objects

    • Objects: Key-value pairs used to store various data types.
    • Properties: Data items belonging to an object, accessed using dot or bracket notation.
    • Methods: Functions belonging to an object, accessed using dot notation.
    • Iteration: for...in loop can be used for looping through object properties.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    JavaScript Basics PDF

    Description

    Test your knowledge of the fundamental concepts in JavaScript with this quiz. Questions cover variable declaration, the use of semicolons, and differences between internal and external scripts. Perfect for beginners looking to reinforce their understanding of JavaScript.

    Use Quizgecko on...
    Browser
    Browser