JS Exam study 1 Flashcards
How do you alert the user?
alert()
How do you print to the console?
console.log()
How do you prompt the user?
prompt()
what is used to declare variables?
var
Javascript is dynamically typed, what does that mean?
A variable can hold a value of any type
Javascript is weakly typed, what does that mean?
JavaScript uses aggressive type conversion so that any expression can be evaluated, regardless of whether the types of the operands match.
What is explicit type conversion and how does it work?
parseInt() returns the integer equivalent of its argument (or NaN)
parseFloat()returns the Float equivalent of its argument (or NaN)
isNaN() returns true if the argument cannot by converted to a numb
What are arrays in javascript?
arrays are objects that can hold a mix of data types.
How do you initialize arrays?
You initialize them using array literals in square brackets or using new Array() var a = []; var a = new Array(); var a = [34, "hi", true]
How do you access elements in an array?
a[1] = 67;
alert(a[1]);
How do you extend an array?
You can extend them simply by assigning to a new index
What to elements that are not defined yet return?
the special value undefined
What is the DOM and what does DOM stand for?
DOM stands for Document Object Model, and it is the browser’s internal representation of the page.
How is the DOM constructed?
The DOM is constructed from the page source as the page is loaded. It consists of a “tree” structure of Node objects. Each Node object represents a single HTML element (paragraph, image, etc.). The DOM can be changed in a JavaScript program after it is constructed.
How does JavaScript properly output data?
A javascript app rarely uses alert, prompt, or confirm, and console.log is for debugging. The “output” of a JavaScript app consists almost entirely of changes to the DOM.
How do you retrieve a node?
var node = document.getElementById("element id goes here") returns node with matching id or null
How do you retrieve an array like structure of nodes?
var nodes= document.querySelectorAll(“css selector goes here”)
returns a an array-like structure of elements that match the selector
can use nodes.length for the size, use a for loop to access each element
for(var i=0; i
How do you change the contents of the node?
use the innerHTML field to change the contents of the node
- node.innerHTML = “New Message”
▪replaces contents with plain text
- node.innerHTML = “<p>New <a>Message</a></p>”
▪replaces contents with new nodes
- node.innerHTML = “”
▪deletes contents
How do you change the style of the node?
use the style field to change CSS properties
- node.style.color = “red”;
- node.style.backgroundColor = “rgb(128, 0, 128)”
▪ note that hyphenated properties are changed to camel case
How do you add or change HTML attributes?
use other fields to change or add HTML attributes
- node.href = “http://www.google.ca”
- node.title = “here’s a popup”
- node.src = “images/newImage.jpg”
- Important Exception…
- To change the class
attribute you have to use
node.className(not
node.class)
- To change the class
What is associative array notation?
JavaScript object fields can be accessed using the dot operator or square brackets
E.g.
- node.style.color = “red”
is the same as
- node.style[“color”] = redoThe square bracket notation is