Intro Flashcards
(53 cards)
What is the syntax to include JS in your HTML and where in the file can it be placed? And to link an external JS file either local in the same folder or a specified folder, or a URL (reason is it’s easier to read/maintain & load faster with cached JS files)?
script tags in head or body or both; script src=”something.js” or “/something/something.js” or “https://something.com/something.js” tags (preferably in head).
What is the common JS syntax/code to change or add to the HTML element content?
document.getElementById(‘demo’).innerHTML = ‘Hello World’;
JS can change HTML attributes: What would the HTML and the JS look like for having a light bulb image with a button on the left & right to turn it on/off by changing the image from gray to colored? First way is all done in HTML, second way is utilizing JS.
HTML will have img id=”lightBulb” src=”bulbOff.jpg” & one button onclick=”document.getElementById(‘lightBulb’).src=’bulbOn.jpg’ “ & another will be onclick=”document.getElementById(‘lightBulb’).src=’bulbOff.jpg’ “
OR
HTML will have img id=”lightBulb” src=”bulbOff.jpg” & one button onclick=”lightOn” & another will be onclick=”lightOff”.
The JS will have lightOn function with content document.getElementById(‘lightBulb’).src=’bulbOn.jpg’; & lightOff function with content document.getElementById(‘lightBulb’).src=’bulbOff.jpg’;
JS can change HTML attributes: What would the HTML look like for a sentence in a paragraph that can increase it’s font size by the click of a button? And then another button to hide (or show) it all together? Can also do this by utilizing JS instead of writing the script in the HTML.
Paragraph tag with a sentence string as the content and id=”demo”.
A button below it saying click me as the content will have the DOM event onclick=”document.getElementById(‘demo’).style.fontSize=’35px’ “.
Another button to hide it will have the DOM event onclick=”document.getElementById(‘demo’).style.display=’none’ “ or ‘block’.
What is a Javascript function?
A block of reusable Javascript code that can be executed when invoked. It can be self invoked by writing its name with parentheses as a statement outside of the function somewhere or called when an event occurs such as clicking a button.
What are some (4) of the different ways JS can display/print data?
the “innerHTML” property can set or overwrite the specified HTML element content
document. write() used only for testing will write into the HTML output position will be where you place it in the HTML file or if its in JS it will be at the top of everything in ascending order, or if it is called as part of a function it will overwrite the current HTML to display something new
window. alert() will display in an alert box
console. log() used for debugging will display in the console
What are JS statements? And what are its 5 different components?
The list of instructions to be executed by the browser (separated by semicolons)
ex- var x; var y = 2; x = 1; z = x + y;
Values (variable & literal), operators, expressions, keywords, and comments.
What is the purpose of variables, how do we declare them, and how do we assign values to them? What is their value after they are declared without anything assigned?
They’re used to store data values; declare using the “var” keyword; use “=” to assign values to them. Their value w/o anything assigned is undefined. (We can use the var keyword just once for multiple identifiers with their individual values in one statement)
What are the different types of operators? Explain.
Arithmetic operators (+, -, *, /, %, **, ++, –); assignment operators (aslo serve as string operators: =, +=, -=, *=, /=, %=); comparison (=== or > or <= or !=); logical (&& or ! or ||); type (typeof, which returns the type of a variable… or instanceof, which returns true if an object is an instance of an object type).
What is a JS expression?
Any combination of values (can even be strings), variables, and operators, which computes to a value.
What are JS keywords?
Used to identify action to be performed (such as “var” tells the browser to create a variable).
What is the syntax for comments in JS? in HTML?
Two forward slashes before a specific line; or, a forward slash+asterisk before desired spot and after an asterisk+forward slash.
What are JS identifiers? And their rules?
Names used to name variables, keywords, functions, and labels. First character must be a letter, underscore, or dollar sign. (Numbers can come after)
What does lower camel case look like? What does upper camel case look like and what is another name for this?
lowerCamelCase; UpperCamelCase - pascal case.
If you put a number in quotes, what happens to the rest of the numbers in the expression after it (in terms of type)? What happens to the output type when any string is being calculated with a number?
The numbers after it will also be treated as strings and concatenated instead of mathematically calculated. Any time a string is in an expression with a number, the output will be a string.
What is the purpose of HTML? CSS? JS?
- HTML to define the content of web pages
- CSS to specify the layout of web pages
- JavaScript to program the behavior of web pages
When was JavaScript invented? By who? And what was its official name?
By Brendan Eich in 1995. ECMAScript.
Use escape characters to alert We are “Vikings”
Backslash before each quotation mark that you wish to display; alert(“We are "Vikings"”);
What are the two best places to break a line of code?
After an operator or a comma.
What are the different data types?
Numbers, strings, objects, booleans, arrays, undefined
What is the term for the pieces inside an array? What is the term to refer to it’s numerical position?
Items. Index number.
What is the term for the pieces inside an object which are separated by commas and written with quotations? What is the term for the first portion of that, and then the second portion which comes after the colon?
Properties. Name: value (name:value pairs).
What is the value of a variable without a value?
Undefined.
What is the data type of null?
An object.