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
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is the correct way to include an external JavaScript file?

  • <link rel="script" href="script.js">
  • <javascript file="script.js"></javascript>
  • <src="script.js"></script>
  • <script src="script.js"></script> (correct)

Which of the following is NOT a component that a JavaScript statement may contain?

  • Operators
  • Keywords
  • Values
  • Procedures (correct)

What is the purpose of the console.log() function in JavaScript?

  • To write content to an HTML element.
  • To display a pop-up alert box.
  • To execute an external script.
  • To display content in the browser's JavaScript console. (correct)

In what year was JavaScript standardized as ECMAScript?

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

Which of the following is the correct way to declare a variable in JavaScript?

<p>let x; (D)</p> Signup and view all the answers

What is one key characteristic of JavaScript?

<p>It is a client-side scripting language. (B)</p> Signup and view all the answers

Which keyword is used to define a function in JavaScript?

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

If you add the number 5 to the string "10", what is the result in JavaScript?

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

What does the typeof operator return?

<p>The data type of a variable. (A)</p> Signup and view all the answers

What is a key characteristic of a loosely typed (or dynamically typed) language like JavaScript?

<p>Variables can store any data type without prior declaration. (D)</p> Signup and view all the answers

JavaScript is an object-oriented programming language.

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

Brendan Eich created JavaScript at Microsoft in 1995.

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

JavaScript was originally named Mocha.

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

JavaScript is directly related to Java.

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

Including JavaScript code in the <head> section is always the best practice.

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

JavaScript code in external files has a .html extension.

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

The innerText property inserts text into an HTML element.

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

JavaScript is case-insensitive.

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

A return statement will stop the execution of the function.

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

Variables declared with const cannot be reassigned.

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

Which of the following best describes a JavaScript object?

<p>A way to store multiple pieces of related data. (A)</p> Signup and view all the answers

Which of the following is an example of an object property?

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

What is the purpose of methods in a JavaScript object?

<p>They define actions that the object can perform. (C)</p> Signup and view all the answers

Given let car = {brand: "Toyota"};, how do you access the brand of the car?

<p><code>car.brand</code> (C)</p> Signup and view all the answers

What does it mean for JavaScript objects to be 'dynamic'?

<p>Their properties can be added, modified, and removed at any time. (D)</p> Signup and view all the answers

Which of the following actions can you perform on a JavaScript object?

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

Which of the following is a built-in JavaScript object?

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

Which of the following represents an array?

<p><code>[]</code> (A)</p> Signup and view all the answers

What is the index of the first element in a JavaScript array?

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

Which JavaScript built-in object is used to work with dates and times?

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

In JavaScript, an object is a way to store multiple pieces of related data.

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

Attributes of an object describe the actions it can perform.

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

Methods define the actions that an object can perform.

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

A JavaScript variable can hold multiple values simultaneously, similar to an object.

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

Object properties can only be accessed using dot notation.

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

JavaScript objects are static and cannot be extended once created.

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

The Math object in JavaScript requires the new keyword to be created.

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

Arrays in JavaScript can only store data of the same type.

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

Arrays in JavaScript are zero-indexed.

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

The JavaScript Date object can only represent the date, not the time.

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

What do control structures determine in a program?

<p>The execution flow (C)</p> Signup and view all the answers

What is the purpose of the else keyword in an if statement?

<p>To specify a block of code to execute if the condition is false (B)</p> Signup and view all the answers

Which statement is used to perform different actions based on different conditions?

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

In a switch statement, what happens when a break keyword is encountered?

<p>Execution exits the switch block (B)</p> Signup and view all the answers

What is the purpose of the default case in a switch statement?

<p>To specify the code to run when no other case matches (B)</p> Signup and view all the answers

What is a key component of a for loop?

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

What is an 'iteration' in the context of loops?

<p>A single execution of the loop's statements (A)</p> Signup and view all the answers

What is the primary characteristic of a while loop?

<p>It executes code as long as a condition remains true. (B)</p> Signup and view all the answers

How does a do...while loop differ from a while loop?

<p>It always executes the code block at least once. (B)</p> Signup and view all the answers

Which keyword is used to exit a loop prematurely?

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

Control structures determine the execution flow of a program.

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

The if statement executes code regardless of the condition.

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

In an if...else statement, the else block executes when the condition is true.

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

A switch statement is used to perform different actions based on different conditions.

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

The break keyword in a switch statement is optional and doesn't affect execution.

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

A loop is a structure that executes a block of code once.

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

In a for loop, the initializer is executed after each iteration.

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

While loops and for loops are post-test loops.

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

A do...while loop always executes its code block at least once.

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

The continue statement exits a loop entirely.

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

What is the name for the hierarchical structure that the browser uses to organize web page components?

<p>Document Object Model (DOM) (A)</p> Signup and view all the answers

Which object is at the top level of the DOM?

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

The Window.screen.width property provides what information?

<p>The total width of the screen in pixels (A)</p> Signup and view all the answers

Which property provides the color depth of the screen?

<p><code>window.screen.colorDepth</code> (C)</p> Signup and view all the answers

Which method displays a simple alert message to the user?

<p><code>window.alert()</code> (A)</p> Signup and view all the answers

Which method can be used to retrieve the title of the current document?

<p><code>document.title</code> (C)</p> Signup and view all the answers

What does the document.domain property return?

<p>The domain name of the document (A)</p> Signup and view all the answers

Which method is best suited for modifying a specific element dynamically?

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

Which of the following methods writes directly into the HTML document?

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

Which property of a checkbox or radio button indicates whether it is selected?

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

The DOM is a hierarchical tree of web page components.

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

The Window.screen.width property returns the available screen width, excluding system UI elements.

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

A color depth of 8-bit allows for millions of colors.

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

JavaScript can display dialog messages using methods provided by the top-level DOM window object.

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

The document.URL property provides access to the page title.

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

document.domain retrieves the domain name of the document.

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

document.forms.length returns the number of forms in the document.

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

Using index positions to access elements is the recommended modern approach.

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

The getElementsByClassArea method is a valid way to reference HTML elements.

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

The innerText property supports text and HTML elements.

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

Flashcards

What is JavaScript?

A scripting language embedded in web browsers for client-side scripting.

What is ECMAScript?

The standard that JavaScript was standardized as in 1997.

What are Language Basics in JS?

Syntax, keywords, operators, and built-in objects.

How to include external JS file?

Using the <script> tag with the src attribute.

Signup and view all the flashcards

What do JS statements contain?

Keywords, operators, values, and expressions.

Signup and view all the flashcards

What are keywords?

Words that have special significance in the JavaScript language.

Signup and view all the flashcards

What are operators?

Special characters that perform an operation on one or more operands.

Signup and view all the flashcards

What are data types in JavaScript?

Number, string, boolean, object, function, symbol, null and undefined

Signup and view all the flashcards

What is a variable?

A container where data can be stored and retrieved later.

Signup and view all the flashcards

What is a function?

A block of code designed to perform a particular task, and returns a value.

Signup and view all the flashcards

Why use JavaScript?

Enables interactivity and faster web experiences directly in the browser, reducing reliance on server-side processing.

Signup and view all the flashcards

How to include JavaScript?

Writing directly within HTML using <script> tags or linking external .js files.

Signup and view all the flashcards

What are values in JS?

Text strings, numbers, booleans, undefined, and null.

Signup and view all the flashcards

What are Expressions?

Units of code that produce a single value when evaluated.

Signup and view all the flashcards

What is a JS statement?

A series of instructions executed in order.

Signup and view all the flashcards

What is whitespace in JS?

Spaces, tabs, and line breaks that improve code readability.

Signup and view all the flashcards

Why use external JS files?

Allows same script use across pages, keeps HTML clean, eases updates.

Signup and view all the flashcards

let vs. const?

Declared with 'let', can change. Declared with 'const', stays fixed.

Signup and view all the flashcards

Ways to write output?

console.log() to browser console, alert() for pop-up, innerText to HTML element.

Signup and view all the flashcards

What is scope in JS?

Determines variable accessibility; either global (everywhere) or local (in function).

Signup and view all the flashcards

What is an Object?

A way to store multiple pieces of related data in a structured format, like a container.

Signup and view all the flashcards

What are Attributes (Properties)?

Describe the characteristics of an object, such as color, brand, and model for a Car object.

Signup and view all the flashcards

What are Behaviors (Methods)?

Define actions that an object can perform, like start(), stop(), and accelerate() for a Car object.

Signup and view all the flashcards

Why Use Objects?

Grouping everything related to a concept or entity in one place.

Signup and view all the flashcards

Dot Notation

Using objectName.propertyName to access the property value.

Signup and view all the flashcards

Bracket Notation

Using objectName['propertyName'] to access the property value.

Signup and view all the flashcards

Are JavaScript Objects dynamic?

Objects in JavaScript are dynamic, Properties can be added, modified, and removed at any time.

Signup and view all the flashcards

List of Built-in Objects

String, Number, Boolean, Object, Date, Array, RegExp, Math, Error.

Signup and view all the flashcards

What is an Array?

Built-in array that stores multiple values in a single variable.

Signup and view all the flashcards

What does the Date Object represent?

Date, time, and timezone information.

Signup and view all the flashcards

What are Object Attributes?

Describes the characteristics of an object.

Signup and view all the flashcards

What are Object Behaviors?

Define what an object can do.

Signup and view all the flashcards

What is a JavaScript Array?

A built-in object which can store different data types (numbers, strings, objects, etc.). Elements are automatically indexed starting from 0

Signup and view all the flashcards

What is Dot Notation?

Accesses property values using a dot.

Signup and view all the flashcards

What is Bracket Notation?

Accesses property values using square brackets.

Signup and view all the flashcards

When to use 'new'?

Used with Date and Error. Avoid with String, Number, or Boolean.

Signup and view all the flashcards

getFullYear()

A JavaScript Date object provides methods to extract the year as four digits

Signup and view all the flashcards

getMonth()

A JavaScript Date object provides methods to extract the month as an index number (0-11)

Signup and view all the flashcards

getDate()

A JavaScript Date object provides methods to extract the day as a number (1-31)

Signup and view all the flashcards

Control Structures

Directs the flow of a program by allowing code execution based on conditions or repetitions.

Signup and view all the flashcards

"If" Statement

Evaluates a condition; if true, executes code.

Signup and view all the flashcards

"Else" Statement

Extends "if" to execute alternative code when the condition is false.

Signup and view all the flashcards

"Switch" Statement

Provides multiple execution paths based on different conditions.

Signup and view all the flashcards

"For" Loop

Repeatedly executes a block of code as long as a condition is true.

Signup and view all the flashcards

"While" Loop

Executes code while a condition remains true

Signup and view all the flashcards

"Do While" Loop

Executes the code block once, then repeats as long as the condition is true.

Signup and view all the flashcards

"Break" Keyword

Exits a loop prematurely when a specified condition is met.

Signup and view all the flashcards

"Continue" Statement

Skips one iteration of a loop and continues with the next.

Signup and view all the flashcards

What are Control Structures?

Control structures determine the order in which statements are executed in a program.

Signup and view all the flashcards

What is the 'else' statement?

Adds a conditional execution path when the initial 'if' condition is false.

Signup and view all the flashcards

What is a 'do while' loop?

A loop that executes the code block at least once, and then continues as long as the condition is true.

Signup and view all the flashcards

What is the 'default' statement in a switch?

A statement in a switch block that executes if none of the case values match the switch expression.

Signup and view all the flashcards

What is a loop structure?

A construct containing a test condition and statements that execute repeatedly while the condition is met.

Signup and view all the flashcards

What is a loop initializer?

Sets the initial value of a loop's counter variable.

Signup and view all the flashcards

What is a loop condition?

An expression evaluated before each loop iteration; loop continues if it returns true.

Signup and view all the flashcards

What is a loop modifier?

A statement that updates a value in the test condition, preventing infinite loops.

Signup and view all the flashcards

What is the Document Object Model (DOM)?

The browser organizes web page components into a hierarchical tree

Signup and view all the flashcards

Why is the DOM Important?

Enables dynamic webpage changes without reloading, allows JavaScript interaction with HTML and CSS, and is essential for interactive web applications.

Signup and view all the flashcards

What is the screen child object?

The top-level DOM object providing properties describing the user's monitor resolution in pixels.

Signup and view all the flashcards

What is Window.screen.width?

Total screen width in pixels.

Signup and view all the flashcards

What is Window.screen.height?

Total screen height in pixels.

Signup and view all the flashcards

What is Window.screen.availWidth?

Available screen width, excluding system UI elements (pixels).

Signup and view all the flashcards

What is Window.screen.availHeight?

Available screen height, excluding system UI elements (pixels).

Signup and view all the flashcards

What is Window.screen.colorDepth?

Bit value indicating the color range (e.g., 8-bit: 256 colors, 32-bit: billions of colors).

Signup and view all the flashcards

What does window.alert(message) do?

Displays a simple alert box with a message and an 'OK' button.

Signup and view all the flashcards

What does window.confirm(message) do?

Displays a confirmation box with 'OK' and 'Cancel' buttons, returning true for 'OK' and false for 'Cancel'.

Signup and view all the flashcards

What does window.prompt(message, defaultValue) do?

A method that displays a prompt box with a text input field, returning the input value with 'OK' or null with 'Cancel'.

Signup and view all the flashcards

What are forms, images, links, styleSheets, and scripts?

Child objects of the document object, represented as arrays corresponding to HTML elements.

Signup and view all the flashcards

How to access element by ID?

Modifies a specific element dynamically by retrieving a single element by its id.

Signup and view all the flashcards

How to access element by tag name?

Changes properties for multiple elements of the same type, by retrieving all elements with the given tag name.

Signup and view all the flashcards

How to access element by class name?

Applies styles or modifications to multiple elements at once by retrieving all elements a specific class.

Signup and view all the flashcards

What is innerHTML?

A method to insert or change HTML content within an element.

Signup and view all the flashcards

What is innerText?

A method to insert or change only plain text within an element, ignoring HTML tags.

Signup and view all the flashcards

setAttribute()

Adds attributes to an HTML element, enabling dynamic modifications to the DOM.

Signup and view all the flashcards

What are Event-handlers?

Functions that are executed when a particular event happens.

Signup and view all the flashcards

Study Notes

Meet DOM

  • A browser organises webpage elements into a hierarchical tree structure.
  • In this structure, the Document Object Model (DOM), each element is nested under the window object.

Inspect Properties

  • The screen object, a child of the window object, holds properties defining the user's monitor resolution, in pixels.

Access Elements

  • Accessing elements via Javascript methods is favourable over array indexing.
  • Array indexing is error-prone when HTML is modified, is tedious in large documents, and is more complex.

Events

  • Responding to events involves the event itself triggering a defined function.
  • Events are handled via HTML Event Attributes for inline use or through JavaScript's addEventListener for event registration on specific targets.

Load Events

  • Load events trigger after a page has fully loaded, prompting functions to execute.

Check Boxes and Radio Buttons

  • Checkboxes allow multiple selections, while radio buttons, grouped with shared names, allow only one selection.
  • Both elements use a .checked property to reflect their selected state.

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

JavaScript Fundamentals and Web Standards
24 questions
JavaScript Basics and Features
40 questions
JavaScript Basics and Evolution
40 questions
Introduction to JavaScript
30 questions
Use Quizgecko on...
Browser
Browser