Introduction to JavaScript Part I
28 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 are the two ways to add JavaScript to Web pages?

  • Include the script in an external file with the `<script>` tag (correct)
  • Use the `<script>` tag (correct)
  • Use the `<javascript>` tag
  • Use the `<java>` tag
  • What is the purpose of the type attribute within the <script> tag?

    text/javascript

    What is the common method to write text to HTML output?

    document.write()

    The document.write() method should be used for production code.

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

    What is the type of JavaScript that is interpreted on the fly?

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

    JavaScript can be incorporated from an external file?

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

    What does the window.alert() function do?

    <p>Writes text into a pop-up window</p> Signup and view all the answers

    Is the console.log() function used to display data in debug mode in the browser?

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

    Which of these keywords is used to declare a variable?

    <p>All of the above</p> Signup and view all the answers

    What does the let keyword represent?

    <p>A block-scoped variable declaration.</p> Signup and view all the answers

    Javascript is case-sensitive?

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

    What does each line end with in JavaScript code?

    <p>A semicolon</p> Signup and view all the answers

    Is it possible to have multiple statements on a single line in Javascript?

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

    What is the purpose of a block in JavaScript?

    <p>To group statements together and define their scope.</p> Signup and view all the answers

    Match the following JavaScript keywords/concepts with their descriptions:

    <p><code>var</code> = Declares a variable that can be redeclared <code>let</code> = Declares a block-scoped variable. Cannot be redeclared <code>const</code> = Declares a block-scoped constant. Value cannot be changed after declaration <code>if</code> = Executes a block of code if a condition is true <code>switch</code> = Executes a code block based on different cases <code>for</code> = Iterates a block of code multiple times <code>function</code> = Defines a reusable block of code <code>return</code> = Exits a function and potentially returns a value <code>try</code> = Handles errors in a block of code <code>catch</code> = Captures errors thrown by the <code>try</code> block <code>finally</code> = Executes a block of code whether an error occurred or not</p> Signup and view all the answers

    Variables declared using let cannot be redeclared.

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

    Variables declared using let can be used outside of the block they were declared in.

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

    What is the purpose of the const keyword in Javascript?

    <p>To declare a constant variable. Its value cannot be changed after the declaration.</p> Signup and view all the answers

    Match the JavaScript Operators with their description:

    <p><code>+</code> = Addition <code>-</code> = Subtraction <code>*</code> = Multiplication <code>**</code> = Exponentiation (raise to the power of) <code>/</code> = Division <code>%</code> = Modulo (remainder of a division) <code>++</code> = Increment (add 1 to the variable) <code>--</code> = Decrement (subtract 1 from the variable)</p> Signup and view all the answers

    What is the purpose of the =operator?

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

    Match the JavaScript assignment operators with their equivalent expressions:

    <p><code>+=</code> = x = x + y <code>-=</code> = x = x - y <code>*=</code> = x = x * y <code>/=</code> = x = x / y <code>%=</code> = x = x % y <code>**=</code> = x = x** y</p> Signup and view all the answers

    What is the difference between the == operator and the === operator?

    <p>The <code>==</code> operator checks for equality without considering the data type, while the <code>===</code> operator checks for both equality and data type.</p> Signup and view all the answers

    JavaScript can only work with numbers?

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

    Can you redeclare a variable declared using var?

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

    What is the output of the following JavaScript code: let x = "5" + 2 + 3; console.log(x);

    <p>&quot;523&quot;</p> Signup and view all the answers

    What is the output of the following JavaScript code: let x = 2 + 3 + "5"; console.log(x);

    <p>&quot;55&quot;</p> Signup and view all the answers

    The output of a Javascript script will always appear in the HTML output?

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

    Signup and view all the answers

    Study Notes

    Introduction to JavaScript - Part I

    • JavaScript is a client-side scripting language.
    • It was developed in just 10 days in May 1995 by Netscape.
    • It was initially called LiveWire and then LiveScript.
    • It runs within the client software (the browser).
    • JavaScript is interpreted on the fly by the client, meaning each line is processed as soon as it's loaded into the browser.
    • Server-side languages, like PHP and Python, run on the web server.
    • JavaScript is significantly easier to learn compared to other programming languages.
    • It's a convenient way to create interactive web pages.

    Topics

    • What is JavaScript?
    • Why JavaScript?
    • Including JavaScript in HTML (two methods):
      • Using the <script>...</script> tag directly within the HTML.
      • Using an external JavaScript file (with <script src="filename.js"></script>).
    • Hello World Example Script: Shows basic JavaScript code to display text on a web page.
    • JavaScript Comments (two types):
      • Single-line comments use //.
      • Multi-line comments use /* ... */.

    Including JavaScript in HTML

    • Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both sections.

    Hello World Example Script

    • The code document.write("<h1>Hello, world!</h1>"); displays "Hello, world!" in an h1 heading on the web page.

    JavaScript Comments

    • Single-line comments start with //.
    • Multi-line comments start with /* and end with */.

    JavaScript Functions

    • JavaScript functions perform specific tasks.
    • A function is a block of code that is designed to perform a particular task.
    • To create a function, use the function keyword.
    • Functions can be called by other parts of a program.
    • A function can have one or many parameters.
    • The document.getElementById("p1").innerHTML ="New Text !"; command displays the text "New Text" in a HTML element with the ID "p1".

    Outputs in JavaScript

    • document.write(): Writes to the HTML page content, but be warned this can overwrite existing content.
    • window.alert(): Displays an alert box.
    • console.log(): Displays output in the browser's developer console (useful for debugging).

    innerHTML Property

    • The innerHTML property is used to update the content of an HTML element.
    • This is done using JavaScript, without affecting the structure of the page.

    Using document.write()

    • The document.write() method writes a string of text to a web page.
    • It should only be used when needed for testing.
    • Using document.write() after the content is loaded, will delete all existing content.

    window.alert()

    • The window.alert() method displays an alert dialog box with the specified text on the screen.

    console.log()

    • The console.log() method displays the specified data in the browser's developer tools' console.

    window.print()

    • The window.print() method prints the current web page.

    JavaScript Statements

    • A JavaScript program consists of a series of statements.
    • Statements are commands or instructions that tell the interpreter what to do.
    • A statement ends in a semicolon.

    JavaScript Syntax

    • JavaScript syntax defines the structure and rules for JavaScript code.
    • A statement often includes variables.
    • Variables are temporary named storage locations to hold data, variables are defined using keywords like let or var.

    Defining JS Variables

    • Javascript variables are declared with var, let, or const.
    • The var keyword was used historically.
    • let and const are newer, introduce block scope, and can not be redeclared.
    • JavaScript identifiers (variable names) are case-sensitive.
    • Several data types exist (strings, numbers, etc.) in JavaScript.

    Let - Block Scope

    • Variables declared with let have block scope - they are only accessible within the block of code they are declared in.

    JavaScript Operators

    • Arithmetic Operators: +, -, *, /, %, **
    • Assignment Operators: =, +=, -=, *=, /=, %=
    • Other Operators: ++, -- (increment, decrement)

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Description

    This quiz covers the basics of JavaScript, initially developed by Netscape in 1995. Learn about its purpose, how to include it in HTML, and the fundamental concepts essential for creating dynamic web pages. Ideal for beginners looking to understand client-side scripting.

    More Like This

    Use Quizgecko on...
    Browser
    Browser