javascript Flashcards

1
Q

Manipulating HTML Elements

A

My First Paragraph

<script>
<br/>	document.getElementById(&quot;demo&quot;).innerHTML=&quot;My First JavaScript&quot;;<br/>
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Writing to The Document Output

A
<script>
<br/>	document.write(&quot;<p>My First JavaScript</p>&quot;);<br/>
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Warning

A
<script>
<br/>	function myFunction()<br/>	{<br/>	document.write(&quot;Oops! The document disappeared!&quot;);<br/>	}<br/>
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
JavaScript Code
 JavaScript code (or just JavaScript) is a sequence of JavaScript statements.

Each statement is executed by the browser in the sequence they are written.

This example will manipulate two HTML elements:

A

document. getElementById(“demo”).innerHTML=”Hello Dolly”;
document. getElementById(“myDIV”).innerHTML=”How are you?”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

JavaScript functions.

A
function myFunction()
 {
 document.getElementById("demo").innerHTML="Hello Dolly";
 document.getElementById("myDIV").innerHTML="How are you?";
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Break up a Code Line

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

JavaScript Variables

A

avaScript variables are “containers” for storing information:

Example

var x=5;
 var y=6;
 var z=x+y;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

JavaScript Variables

A

As with algebra, JavaScript variables can be used to hold values (x=5) or expressions (z=x+y).

Variable can have short names (like x and y) or more descriptive names (age, sum, totalvolume).

Variable names must begin with a letter
Variable names can also begin with $ and _ (but we will not use it)
Variable names are case sensitive (y and Y are different variables)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

JavaScript Data Types

A

JavaScript variables can also hold other types of data, like text values (name=”John Doe”).

In JavaScript a text like “John Doe” is called a string.

There are many types of JavaScript variables, but for now, just think of numbers and strings.

When you assign a text value to a variable, put double or single quotes around the value.

When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.

Example

var pi=3.14;
 var name="John Doe";
 var answer='Yes I am!';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

One Statement, Many Variables

A

You can declare many variables in one statement. Just start the statement with var and separate the variables by comma:

var name="Doe", age=30, job="carpenter";
 Your declaration can also span multiple lines:
var name="Doe",
 age=30,
 job="carpenter";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

JavaScript Has Dynamic Types

A

JavaScript has dynamic types. This means that the same variable can be used as different types:

Example

var x                // Now x is undefined
 var x = 5;           // Now x is a Number
 var x = "John";      // Now x is a String
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

JavaScript Numbers

A

JavaScript has only one type of numbers. Numbers can be written with, or without decimals:
Example

var x1=34.00;      //Written with decimals
 var x2=34;         //Written without decimals
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

JavaScript Booleans

A

Booleans can only have two values: true or false.

var x=true
 var y=false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

JavaScript Arrays

A
var cars=new Array();
 cars[0]="Saab";
 cars[1]="Volvo";
 cars[2]="BMW";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

JavaScript Objects

A

An object is delimited by curly braces. Inside the braces the object’s properties are defined as name and value pairs (name : value). The properties are separated by commas:

var person={firstname:"John", lastname:"Doe", id:5566};
 The object (person) in the example above has 3 properties: firstname, lastname, and id.

Spaces and line breaks are not important. Your declaration can span multiple lines:

var person={
firstname : “John”,
lastname : “Doe”,
id : 5566
};
You can address the object properties in two ways:

Example

name=person.lastname;
name=person[“lastname”];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

JavaScript Objects

Properties and Methods

A

Properties are values associated with an object.

Methods are actions that can be performed on objects.

17
Q

Objects in JavaScript:

A

In JavaScript, objects are data (variables), with properties and methods.

When you declare a JavaScript variable like this:

var txt = "Hello";
 You actually create a JavaScript String object. The String object has a built-in property called length. For the string above, length has the value 5. The String object also have several built-in methods.

Properties:
txt.length=5
“Hello”
Methods:
txt.indexOf()

txt. replace()
txt. search()

In object oriented languages, properties and methods are often called object members.

You will learn more about properties and the methods of the String object in a later chapter of this tutorial.

18
Q

Creating JavaScript Objects

A

Almost “everything” in JavaScript is an object. Strings, Dates, Arrays, Functions.

You can also create your own objects.

This example creates an object called “person”, and adds four properties to it:

Example

person=new Object();

person. firstname=”John”;
person. lastname=”Doe”;
person. age=50;
person. eyecolor=”blue”;

19
Q

Accessing Object Properties

A

The syntax for accessing the property of an object is:

objectName.propertyName
This example uses the length property of the String object to find the length of a string:

var message="Hello World!";
 var x=message.length;
 The value of x, after execution of the code above will be:

12

Accessing Object Methods
You can call a method with the following syntax:

objectName.methodName()
This example uses the toUpperCase() method of the String object, to convert a text to uppercase:

var message="Hello world!";
 var x=message.toUpperCase();
 The value of x, after execution of the code above will be:

HELLO WORLD!

20
Q

JavaScript Functions

A

A function is a block of code that will be executed when “someone” calls it:

Example

<script>
<br/>	function myFunction()<br/>	{<br/>	alert(&quot;Hello World!&quot;);<br/>	}<br/>
</script>

<button>Try it</button>

JavaScript Function Syntax
A function is written as a code block (inside curly { } braces), preceded by the function keyword:

function functionname()
 {
 some code to be executed
 }
 The code inside the function will be executed when "someone" calls the function.

The function can be called directly when an event occurs (like when a user clicks a button), and it can be called from “anywhere” by JavaScript code.

JavaScript is case sensitive. The function keyword must be written in lowercase letters, and the function must be called with the same capitals as used in the function name.

21
Q

Calling a Function with Arguments

A

When you call a function, you can pass along some values to it, these values are called arguments or parameters.

These arguments can be used inside the function.

You can send as many arguments as you like, separated by commas (,)

myFunction(argument1,argument2)
Declare the argument, as variables, when you declare the function:

function myFunction(var1,var2)
 {
 some code
 }
 The variables and the arguments must be in the expected order. The first variable is given the value of the first passed argument etc.

Example

<button>Try it</button>

<script>
<br/>	function myFunction(name,job)<br/>	{<br/>	alert(&quot;Welcome &quot; + name + &quot;, the &quot; + job);<br/>	}<br/>
</script>
22
Q
A