JavaScript Flashcards
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
What is important to know about JavaScript variables?
Variables are used to store data values.
The keyword “var” is used to declare variables (without an “=”).
Use “=” to assign values to variables.
All variables must be identified with unique names which are called identifiers.
How do you write comments?
Code after // or between /* and */.
Is JavaScript case sensitive?
Yes. (e.g. JavaScript does not interpret VAR or Var as the keyword var)
What can you tell me about hyphens in JavaScript?
They are reserved for substractions and not allowed in names.
What are alternatives to “var” since 2015 and how are they used?
+ const = defines a variable that cannot be reassigned
+ let = defines a variable with restricted scope
How do you write the “equal to” operator in JavaScript?
”==”
“=” is an assignment operator
How do you declare a variable and assign a value to it?
var carName;
carName = “Volvo”;
or
var carName = “Volvo”;
You can declare many variables seperated by comma: var person = "John Doe", carName = "Volvo", price = 200;
Without assignment of a value to a variable, that variable will have the value “undefined”.
How are $ and _ treated in JavaScript?
They’re both treated as letters and can therefore be used as a start of a variable name (numbers are not allowed as the start of an identifier).
What is the Global Scope in JavaScript?
Variables declared globally (outside any function) have global scope and therefore can be accessed from anywhere in a JavaScript program.
What is the Function Scope in JavaScript?
Variables declared locally (inside a function) have Function Scope and can only be accessed from inside the function where they are declared.
What is a key difference between let and var when it comes to scope?
Varibales declared with the var keyword cannot have Block Scope. When they’re declared inside a block { } they can be accessed from outisde the block.
Varibales declared with the let keyword can have Block Scope and therefore cannot be accessed from outside the block.
Using “let” to redeclare a variable inside a block can be helpful.
Global variables defined with let cannot be used for the window command.