JavaScript Introduction

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Listen to an AI-generated conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

DHTML is a combination of which technologies?

  • কেবল XML and JavaScript
  • HTML, Java, and PHP
  • HTML, CSS, and JavaScript (correct)
  • কেবল HTML and CSS

Which of the following is NOT a capability of JavaScript?

  • Manipulating the DOM
  • Validating form data
  • Directly accessing server-side filesystems (correct)
  • Handling events

In which parts of an HTML document can JavaScript code be implemented?

  • sowohl the `<head>` and `<body>` sections
  • In the `<head>` section only
  • In the `<body>` section only
  • In the `<head>`, in the `<body>`, or in external .js files (correct)

What does AJAX stand for in web development?

<p>Asynchronous JavaScript and XML (A)</p>
Signup and view all the answers

Which statement is true regarding external JavaScript files?

<p>External JavaScript files are highly recommended and get cached by the browser. (A)</p>
Signup and view all the answers

When is JavaScript code executed in relation to the loading of a webpage?

<p>During page loading or when the browser fires an event (C)</p>
Signup and view all the answers

How can function calls or code be attached to specific actions or events in HTML?

<p>Using 'event handlers' via tag attributes (A)</p>
Signup and view all the answers

Given the following code, what will be the output in an alert box? alert("9" + 9);

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

What is the purpose of the parseInt() function in JavaScript?

<p>To convert a string to an integer (B)</p>
Signup and view all the answers

Which method is used to add a new element to the end of an array in JavaScript?

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

What are the basic types of popup boxes available in JavaScript?

<p>Alert, Confirm, and Prompt (D)</p>
Signup and view all the answers

Which of the following is the correct syntax for an if statement in JavaScript?

<p>if (i == 5) (A)</p>
Signup and view all the answers

How do you declare a function in JavaScript?

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

Which statement about JavaScript functions is correct?

<p>Functions have access to all passed arguments via the <code>arguments</code> array. (C)</p>
Signup and view all the answers

What does DOM stand for in JavaScript context?

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

How can you access an HTML element with the ID "myElement" using JavaScript?

<p><code>document.getElementById(&quot;myElement&quot;)</code> (A)</p>
Signup and view all the answers

What is the purpose of the className property in JavaScript?

<p>To read or set the class attribute of an HTML element. (C)</p>
Signup and view all the answers

Which property allows you to modify the CSS styles of an element directly in JavaScript?

<p><code>element.style</code> (D)</p>
Signup and view all the answers

How can you find the first child of an element using DOM manipulation in JavaScript?

<p><code>element.firstChild</code> (D)</p>
Signup and view all the answers

What happens when you register an event handler in JavaScript?

<p>The JavaScript code waits passively for the event to occur and then runs the specified function. (D)</p>
Signup and view all the answers

Which of the following is NOT a common DOM event related to mouse interactions?

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

Which event is specifically designed to execute when all the content on the page has been fully loaded?

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

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

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

What does the navigator object provide access to in JavaScript?

<p>Information about the user's browser (B)</p>
Signup and view all the answers

Which JavaScript object contains information about the user's screen resolution?

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

What is the purpose of the document.location object in JavaScript?

<p>To access and manipulate the currently open URL. (C)</p>
Signup and view all the answers

What is the primary use for the Math object in JavaScript?

<p>Performing mathematical operations. (B)</p>
Signup and view all the answers

Which method of the Math object returns a random number between 0 (inclusive), and 1 (exclusive)?

<p><code>Math.random()</code> (A)</p>
Signup and view all the answers

What is the purpose of the setTimeout() function in JavaScript?

<p>To execute a function once after a specified delay. (A)</p>
Signup and view all the answers

What is the difference between setTimeout() and setInterval() in JavaScript?

<p><code>setTimeout()</code> executes code once while <code>setInterval</code> executes code repeatedly at fixed intervals. (C)</p>
Signup and view all the answers

Debugging JavaScript can be achieved using which of the following tools?

<p>Browser developer tools, Microsoft Script Editor, and Firebug (B)</p>
Signup and view all the answers

Which of the following console object methods can be used to write log messages at runtime when debugging?

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

Which of the following is a benefit of using JavaScript?

<p>It reduces server load by performing operations client-side. (B)</p>
Signup and view all the answers

What is a common use case for JavaScript in modern web development?

<p>Adding interactivity and dynamic content to webpages (A)</p>
Signup and view all the answers

How does JavaScript enhance the overall user experience on websites?

<p>By enabling dynamic content updates and interactivity without full page reloads (D)</p>
Signup and view all the answers

What is the primary role of JavaScript in the context of DHTML?

<p>To add dynamic behavior and interactivity to web pages. (B)</p>
Signup and view all the answers

What type of programming language paradigm can JavaScript be used as?

<p>An object-oriented language (A)</p>
Signup and view all the answers

Which of the following tasks is best suited for JavaScript?

<p>Adding interactive maps to a webpage (A)</p>
Signup and view all the answers

Which statement is correct about a variable in Javascript?

<p>Javascript variables can be declared without types (D)</p>
Signup and view all the answers

Which is the correct file extension for Javascript files?

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

Flashcards

What is JavaScript?

A language that enables dynamic behavior on web pages, making them interactive and responsive to user actions.

What is the DOM?

A model that treats HTML documents as a structured tree, allowing JavaScript to manipulate the page's content, style, and structure.

What is AJAX?

A technique for creating faster, more interactive web applications by exchanging data with a server asynchronously, updating parts of a web page without reloading the entire page.

What does 'var' do?

Defines a new variable

Signup and view all the flashcards

JavaScript Operators

Operators commonly used in JavaScript for calculations and comparisons.

Signup and view all the flashcards

Boolean

A data type representing logical quantities.

Signup and view all the flashcards

String

A data type representing text.

Signup and view all the flashcards

JavaScript Pop-up boxes

Commands to display simple dialog boxes to interact with users.

Signup and view all the flashcards

If Statement

A statement that executes a block of code only if a specified condition is true.

Signup and view all the flashcards

Switch Statement

A statement that executes one of many blocks of code based on the value of a variable.

Signup and view all the flashcards

Loops

Control flow structures that repeat a block of code.

Signup and view all the flashcards

Function

Reusable block of code that performs a specific task.

Signup and view all the flashcards

Debugging

The process of identifying and removing errors from computer hardware or software.

Signup and view all the flashcards

What is Firebug?

A Firefox add-on tool used for JS debugging.

Signup and view all the flashcards

setTimeout()

The object lets you execute a function, after a specified number of milliseconds.

Signup and view all the flashcards

setInterval()

The method calls a function or evaluates an expression at specified intervals (in milliseconds).

Signup and view all the flashcards

What is JavaScript?

JavaScript's front-end scripting is lightweight, client-side, flexible and powerful.

Signup and view all the flashcards

What is DHTML?

A combination of HTML, CSS, and JavaScript to create dynamic web pages

Signup and view all the flashcards

What is DOM?

A standard for accessing and manipulating document content.

Signup and view all the flashcards

What does Javascript do?

Allows interactivity on web pages

Signup and view all the flashcards

Study Notes

Introduction to JavaScript

  • JavaScript is commonly used with HTML and CSS in DHTML

Table of Contents

  • DHTML
  • JavaScript introduction, implementation
  • JavaScript syntax
  • Document Object Model
  • Debugging

What is DHTML?

  • DHTML means Dynamic HTML
  • Enables web pages react and change on user actions
  • DHTML is a combo of HTML, CSS, and JavaScript

HTML, CSS, JavaScript

  • HTML defines content
  • CSS defines rules / styles
  • JavaScript defines behavior

What is JavaScript?

  • Front-end scripting language from Netscape for dynamic content
  • Lightweight, but limited
  • Simple and flexible
  • Powerful to manipulate the DOM
  • Can be object-oriented

JavaScript Advantages

  • Allows interactivity, such as form validation and AJAX functionality

What Can JavaScript Do?

  • Handle events
  • Modify the DOM
  • Validate form data
  • Modify browser cookies
  • Detect browser and OS
  • Handle exceptions
  • Asynchronous server calls (AJAX)

AJAX

  • AJAX = Asynchronous JavaScript and XML
  • New technique for more interactive web apps using XML, HTML, CSS, and JavaScript
  • AJAX uses XHTML, CSS, DOM, and JavaScript

How AJAX works

  • A typical web application transmits information serially
  • AJAX uses JavaScript to request data after submission, and updates the current screen

Using JavaScript Code

  • JavaScript code can be placed in the head or body
  • Usually external linked files using a script tag in the head are used
  • .js files are cached by the browser

JavaScript Execution

  • JavaScript executes during page loading or when the browser fires an event
  • Function calls or code attached via tag attributes

JavaScript Syntax

  • The JavaScript syntax is similar to C# and Java
  • Typeless variables
  • Common Operators (+, *, =, !=, &&, ++, ...)
  • Conditional statements (if, else)
  • Loops (for, while)
  • Arrays (my_array[]) and associative arrays (my_array [‘abc’])
  • Functions (can return value)
  • Function variables (like the C# delegates)

JavaScript Data Types

  • Numbers
  • Boolean (true / false)
  • String
var myName = "You can use both single or double quotes for strings";
var my_array = [1, 5.3, "aaa"];
var my_hash = {a:2, b:3, c:"text"};
  • Arrays, Associative arrays

JavaScript: Everything is an Object

  • Every JavaScript variable can be considered an object

String Operations

Adding Strings

string1 = "fat ";
string2 = "cats";
alert(string1 + string2); // fat cats

What is "9" + 9?

alert("9" + 9); // 99

Converting string to number

alert(parseInt("9") + 9); // 18

Arrays in JavaScript

var arr = new Array();
var arr = [1, 2, 3, 4, 5];
arr.push(3);
var element = arr.pop();
arr.length;
arr.indexOf(1);

Standard Popup Boxes

Alert box

alert("text");
  • Confirmation box (OK / Cancel)
confirm("Are you sure?");
  • Prompt box (text, input field)
prompt ("enter amount", 10);

Conditional Statement

unitPrice = 1.30;
if (quantity > 100) {
unitPrice = 1.20;
}
>	Greater than
<	Less than
>=	Greater than or equal to
<=	Less than or equal to
==	Equal
!=	Not equal

Conditional Statement (if)

  • The condition may be of Boolean or integer type

Switch Statement

  • The switch statement functions like C#

Loops and Functions

  • JavaScript, like C#, has for, while, and do...while loops

JavaScript Functions

  • JavaScript functions can split code into parts
  • Is used to processes data, and return a response

Function Arguments and Return Values

  • Functions are optional
function sum() {
var sum = 0;
for (var i = 0; i < arguments.length; i ++)
sum += parseInt(arguments[i]);
return sum;
}
alert(sum(1, 2, 4));

Document Object Model - DOM

  • Every HTML element is accessible via the JavaScript DOM API
  • Most DOM objects can be manipulated by the programmer

DOM: The Event Model

  • The event model lets the document react to user actions
  • Advantages: Create interactive pages, update objects without reload

How to Access DOM Elements

var elem = document.getElementById("some_id")
var arr = document.getElementsByName("some_name")
var imgTags = el.getElementsByTagName("img")

How to Manipulate the DOM

function change(state) {
var lampImg = document.getElementById("lamp");
lampImg.src = "lamp_" + state + ".png";
var statusDiv = document.getElementById("statusDiv");
statusDiv.innerHTML = "The lamp is " + state";
}
...
<img src="test_on.gif" onmouseover="change('off')"
onmouseout="change('on')" />

Common Element Properties

  • Most properties come from the HTML attributes of the tag.
  • Example: id, name, href, alt, title, src, className
  • The style property modifies the CSS

How to Access Elements through the DOM Tree Structure

  • element.childNodes
  • element.parentNode
  • element.nextSibling
  • element.previousSibling
  • element.firstChild
  • element.lastChild

The HTML DOM Event Model

<img src="logo.gif" onclick="alert('clicked!')" />

is the same as

var img = document.getElementById("logo");
img.onclick = imageClicked;

HTML DOM Event Model: Event Handlers

  • Event handlers receive one parameter that brings information about the event
  • Includes type (mouse click, key press, etc)
  • Location
  • Reference to the event sender

Common DOM Events

  • Mouse Events: onclick, onmousedown, onmouseup
  • Key Events: onkeypress, onkeydown, onkeyup
  • Interface Events: onblur, onfocus, onscroll
  • Form Events: onchange, onsubmit
  • Miscellaneous Events: onload, onunload

Built-In Browser Objects

  • The browser provides some read-only data
  • Commonly used: window, document, screen, browser

JavaScript Debugging

  • JavaScript console reports errors in scripts
  • Use Microsoft Script Editor, and Firebug

Studying That Suits You

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

Quiz Team

Related Documents

JavaScript Basics PDF

More Like This

Use Quizgecko on...
Browser
Browser