WebDesign JScript Flashcards
What is the history of JavaScript? (4)
- was invented by Brendan Eich in 1995
- became an ECMA standard in 1997
- ECMA-262 is the official name of the standard
- ECMAScript is the official name of the language
How is JScript code inserted into a HTML page? (3)
- between < script > < / script > tags
- either in the < head > or at the end of the < body > section, or in both
- can be stored in external js.file and linked to
Why is it preferrable to include JavaScript code at the bottom of the < body > section?
~improves display time as script compilation slows down the display
Place a script code in the < body > changing the text of a paragraph into some other text if you press a button.
< button type=”button” onclick=”myFunction()” >Try it< / button >
< script >
function myFunction() {
document.getElementById(“demo”).innerHTML = “Paragraph changed.”;
}
< / script >
Put the link to an external JScript file “myScript.js” onto a homepage.
< body >
< script src=”myScript.js” >< / script >
< / body >
What are the advantages of using external .js files? (4)
- practical when the same code is used in many different web pages
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
What are the 4 different ways JS output can be displayed?
- Writing into an HTML element, using innerHTML.
- Writing into the HTML output using document.write().
- Writing into an alert box, using window.alert().
- Writing into the browser console, using console.log().
Use a simple example to show how innerHTML is used?
< p id=”test” >< / p >
< script >
document.getElementById(“test”).innerHTML = 5 + 6;
< / script >
Create an alert prompt with “Hello World!” displayed.
< script >
window.alert(“Hello World!”);
< / script >
Calculate 5 + 6 and display the result in the console.
< script >
console.log(5 + 6);
< / script >
Use document.write when clicking on a button, displaying “It’s all gone!”
< button onclick=”document.write(“It’s all gone!”) >Try it< / button >
- document.write after a page has fully loaded will erase all existing HTML
- use only for testing
JavaScript statements are composed of… (5)
- Values
- Operators
- Expressions
- Keywords
- Comments
The JavaScript syntax defines two types of values: … values and … values.
… values are called … … values are called …
Fixed, variable, Fixed, literals, Variable, variables.
What rules exist for literals in JScript?
- numbers can be written with or without decimals (type casting not required)
- strings can be written using double or single quotes
How are variables declared in JS?
-there are no types, all variables are declared with var
< script >
var x;
x = 6;
document.getElementById(“demo”).innerHTML = x;
< / script >
What can JScript expressions contain?
Values, variables, and operators.
Strings and operators: “John” + “ “ + “Doe
Variables and number literals: x + 10
How do you comment in JScript?
// for single line comments
/* */ for multiline comments
Are lastname and lastName the same thing?
No, JScript is case sensitive.
What is VAR?
A unique name; it is not the keyword var!
What character set does JScript use?
Unicode
Most JavaScript programs contain many JavaScript …
They are executed, one by one, in the … order as they are written.
JavaScript programs (and JavaScript statements) are often called …
statements, same, JavaScript code
The best place to break in a JScript statement is after …
…an operator.
What is the keyword break for?
Terminates a switch or a loop.
What is the continue keyword for?
Jumps out of a loop and starts at the top.
What is the debugger keyword for?
Stops the execution of JavaScript, and calls (if available) the debugging function.
What is the return keyword for?
Exits a function.
All JavaScript variables must be identified with … Those names are called …
unique names, identifiers
What are the rules for variable identifiers? (5)
- can contain letters, digits, underscores, and dollar signs
- must begin with a letter
- can also begin with $ and _
- names are case sensitive (y and Y are different variables)
- reserved words (like JavaScript keywords) cannot be used as names
Declare a variable with the value “Cheetah”.
var fastAnimal = “Cheetah”;
Declare the following variables in one statement.
Amano
56 art books
price $112
var artist = “Amano”, booksPublished = 56, avePrice = 112;
OR
var artist = “Amano”,
booksPublished = 56,
avePrice =112;
A variable that has no value declared has the value …
undefined
Which arithmetic operators are allowed in JScript? (7)
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Increment
- Decrement
Give the 5 shortcut operators you can use in JScript.
x += y shortcut for x = x + y
x -= y shortcut for x = x - y
x *= y shortcut for x = x * y
x /= y shortcut for x = x / y
x %= y shortcut for x = x % y
How can you use a shortcut operator to concatenate string?
We are gonna / party thru the night.
txt1 = “We are gonna “;
txt1 += “party thru the night.”;
Which comparison operators does JScript know? (9)
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
What logical operators does JScript know? (3)
&& logical and
|| logical or
! logical not
What does typeof do?
Returns the type of a variable.
What does instanceof do?
Returns true if an object is an instance of an object type.
bitwise
bitwise
The numbers (in an arithmetic operation) are called …
…operands.
The operation (to be performed between the two operands) is defined by an …
…operator.
What will be displayed?
var x = 97 + “Chicken”;
97Chicken
JScript evaluates from … to …
What will be displayed?
var x = 13 + 8 + “Miami”;
left, right
21Miami
JScript first reaches the numbers and calculates 13 + 8. It then reaches the string “Miami” and converts all into a string.
What will be displayed?
var x = “Baby” + 11 + 8;
Baby118
JScript first reaches the string. It then converts all into string, never calculating 11 * 8.
How can you display quotes inside a string?
The quotes must not match!
var text = "The cat is called 'Pixie'"; var text = 'The cat is called "Pixie"';
How do you break to a new line in a string?
< script >
var plant1 = “Clover”, plant2 = “Dandelion”;
document.getElementById(“test”).innerHTML =
plant1 + “
“ +
plant2;
var firstName = ‘Johnny’, lastName = ‘Flynn’, occupation = ‘Carpenter’;
document.getElementById(“demo”).innerHTML =
firstName + “ “ +
lastName + “
“ +
occupation;
< / script >
How can you write the following numbers in scientific notation?
3670000000
0.000056
367e7
56e-6
Write a simple array with the following items:
wallaby
kangaroo
koala
var aussieMammals = [“wallaby”, “kangaroo”, “koala”];
Display the following list.
Jeep
Hummer
Landcruiser
\< script \> var cars = ["Jeep","Hummer","Landcruiser"];
document.getElementById(“test”).innerHTML = cars[0] +
+ “< br >” + cars[1] + “< br >” + cars[2];
< / script >
Declare an object, which has the following properties:
55-year-old man
Wayne Price
Cricket
green eyes
var person = {
firstName: “Wayne”,
lastName: “Price”,
Sex: “m”,
age: 55,
eyeColor: “green”,
favSport: “Cricket”};
The typeof operator returns “…” for arrays because in JavaScript arrays are …
object, objects
What is the difference between undefined and null? (3)
~ undefined’s type is undefined, null’s type is object
undefined == null yields “true”
undefined === null yields “false”
What is the syntax of functions in JScript?
function name(parameter1, parameter 2, parametern) {
code to be executed
}
Function arguments are the … received by the function when it is invoked.
Inside the function, the arguments (…) behave as … variables.
A Function is much the same as a … or a …, in other programming languages.
real values, the parameters, local, Procedure, Subroutine
In JavaScript null is “…”. It is supposed to be something that doesn’t exist.
Unfortunately, in JavaScript, the data type of null is an …
You can consider it a … in JavaScript that typeof null is an … It should be null.
You can … an object by setting it to …
nothing, object, bug, object, empty, null or undefined
When are JScript functions invoked? (3)
- When an event occurs (when a user clicks a button)
- When it is invoked (called) from JavaScript code
- Automatically (self invoked)
Create a very simple function “calculate” that calculates 4 * 3.
< script >
function calculate(a, b) { return a \* b; } document.getElementById("demo").innerHTML = myFunction(4, 3); function someFunction(a,b) {
return a * b;
document.getElementById(“test”).innerHTML = (4,3);
< / script >
Fix the function to display “Hello Scotty”.
< p id=”test” >Text text text.< / p >
< script >
function myFunction(name) {
return “Hello “ + name;
}
< / script >
function myFunction(name) { return "Hello " + name; }
< p id=”test” >Text text text.< / p >
< script >
function myFunction(name) {
return “Hello “ + name;
}
document.getElementById(“test”).innerHTML = myFunction(“Scotty”);
< / script >
function myFunction(name) { return "Hello " + name;
}
document.getElementById(“test”).innerHTML = myFunction(“Scotty”);