JavaScript Flashcards
(88 cards)
Give an example of how to use a method to “find” an HTML element and change the element content?
document.getElementById(“demo”).innerHTML = “Hello JavaScript”;
What would be the code for having an image of a light bulb turn on and off with the fitting buttons?
Turn on the light
<img></img>
Turn off the light
In what sorts can JavaScript change HTML?
+ It can change the HTML content (e.g. with document.getElementById(“demo”).innerHTML = …”
+ It can change HTML Attribute Values (e.g. the src attribute of an <img></img> tag - lightbulb example)
+ It can change the HTML Styles (CSS) (e.g. document.getElementById(“demo”).style.fontSize = “35px”;
+ It can hide HTML elements (e.g. “document.getElementById(“demo”).style.display” = “none”)
+ It can show HTML elements (e.g. “document.getElementById(“demo”).style.display = “block”;
What is ECMA-262?
It’s the official name of the standard.
ECMAScript is the official name of the language.
ECMA is a standards organization for information and communication systems. It acquired its current name in 1994, when the European Computer Manufacturers Association (ECMA) changed its name to reflect the organization’s global reach and activities.
Where does the JavaScript code live in your HTML page?
It’s inserted between and .
You can place any number of scripts in an HTML document.
Scripts can be placed in the or in the >head> section of an HTML page, or in both.
What’s the benefit of placing scripts in the body instead of in the head?
It improves the display speed because script interpretation slows down the display.
How do you use external JavaScript files and what are their advantages?
The external files end with .js and are referred to in your HTML document as “ depending on its location or by referencing a full URL.
Advantages:
+ practical when same code is used in many different web pages
+ it seperates HTML and code
+ IT makes HTML and JavaScript easier to read and maintain
+ Cahced JavaScript files can speed up page loads
What are the different JavaScript display possibilities?
+ writing into an HTML element, using innerHTML
+ writing into the HTML output using document.write()
+ writing into an alert box, using window.alert() - here you can also leave out the “window” keyword.
+ writing into the browser console, using console.log()
What do you have to keep in mind with document.write()?
Using document.write() after an HTML document is loaded, will delete all existing HTML.
It should only be used for testing purposes.
What are JavaScript Programs?
A computer program is a list of “instructions” to be “executed” by a computer.
In a programing language, these programming instructions are called statements.
A JavaScript program (or JavaScript code) is a list of programming statements.
What are JavaScript statements composed of?
JavaScript statements are composed of \+ values \+ operators \+ expressions \+ keywords \+ comments
What are JavaScript Code Blocks and how are they indicated?
JavaScript statements can be grouped together in code blocks, inside curly brackets. They define statements to be executed together (e.g. in functions).
What does “break” do?
Terminates a switch or a loop.
What does “continue” do?
Jupos out of a loop and starts at the top.
What does “debugger” do?
Stops the execution of JavaScript and calls (if available) the debugging function.
What does “do…while” do?
Executes a block of statements and repeats the block while a condition is true.
What does “for” do?
Marks a block of statements to be executed, as long as a condition is true.
What does “function” do?
Declares a function.
What does “if…else” do?
Marks a block of statements to be executed, depending on a cndition.
What does “return” do?
Exits a function.
What does “try…catch” do?
Implements error handling to a block of statements.
What does “var” do?
Declares a variable.
What kind of JavaScript values do exist?
There are 2 types:
+ fixed values: Literals
+ variable values: Variables
What are the two most important syntax rules for fixed values?
- Numbers are written with or without decimals
2. Strings are text, written within double or single quotes
- ";
for (i = 0; i < fLen; i++) {
text += "
- " + fruits[i] + " "; } text += "
- ";
fruits.forEach(myFunction);
text += "
"; } ``` Array.map() creates a new array by performing a function on each array element (it does not change the original array) ``` e.g. var numbers1 = [45, 4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); ``` ``` function myFunction(value, index, array) { return value * 2; } ``` Array.filter() creates a new array with array elements that passes a test; ``` e.g. var numbers = [45, 4, 9, 16, 25]; var over18 = numbers.filter(myFunction); ``` ``` function myFunction(value, index, array) { return value > 18; } ``` Array.reduce() runs a function on each array element to produce (reduce it to) a single value (it does not reduce the original array) ``` e.g. var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduceRight(myFunction); ``` ``` function myFunction(total, value, index, array) { return total + value; } ``` Array.every() checks if all array values pass a test ``` e.g. var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.every(myFunction); ``` ``` function myFunction(value, index, array) { return value > 18; } ``` similar to this: some() --> checks if some array values pass a test Array.indexOf() searches an array for an element value and turns its position Array.lastIndexOf() same but returns the position of the last occurrence of the specified element Array.find() returns the value of the first array element that passes a test function Array.findIndex() returns the index of the first array element that passes a test function
"; }
"; }