JAVASCRIPT Flashcards
Where does JavaScript run?
In the browser, no need for a development environment.
What is built-in the browser to run JavaScript code?
The JavaScript console.
How do you open the JavaScript console?
ctrl+shft+J
How do you clear the console?
by pressing the circular icon with the bar running through it.
What is a line of JavaScript code called?
A JavaScript statement.
How to bring up a pop-up box with a statement on it?
alert();
How to bring up the previous command on the console?
use the up arrow.
Code for a dialog-box with the words “hi” …
alert(“hi”);
Code to send a message to the console with the word “Hello” …
console.log(“Hello”);
Code to send the text “Hi” to the webpage you’re on …
document.write(“Hi”);
How do you to the next line in the console without entering your current line of code?
shft + return
Code to write an < H1 > with the text “Fuck You” to the current webpage you’re on …
document.write( “ < h1 > Fuck You < / h1 > “ );
How does code run?
line by line
Where do you place JavaScript code?
In a seperate file, but in the same folder of your html, much like a CSS file.
What executes JavaScript code?
The JavaScript engine built into the browser.
What extension do JavaScript files have?
.js
Where are JavaScript links usually placed in HTML files?
Just before the ending < body > tag.
Code for a link from an html file to a JavaScript file named script.js which is in a file named js …
< script src = “ js / script.js “ > < / script >
Can you link more than one JavaScript file in your html file?
YES, YES a thousand times YES, ALREADY!!!
Where are JavaScript errors logged?
In the JavaScript console.
What info will be listed on the error line in the console?
the file and line of error
Is JavaScript case sensitive?
YES
How do you write a JavaScript comment?
// till eol, end of line
/* mutline
like
this */
What are the three ways you can declare a JavaScript variable?
var
let
const
Declare a variable named score …
var score;
const score;
let score;
Declare and assign a value, 0, to the variable named score …
var score = 0;
let score = 0;
const score = 0;
What is meant by “reassigning a variable”?
It is when you overwrite a value in a variable with a new value.
var score = 0;
score = 1;
notice you don’t have to write var twice, which is nice.
Which type of variable can you not use to reassign a value to?
const
What are some of the rules for naming variables?
- Variable names cannot start with a number. (9lives, no good).
- Names can only contain letters, numbers, and the $ and _ characters.
- Use camelCase for multiWord variable names.
- Make variable names meaningful.
- Don’t use KW’s.
Code an example of the addition assignment operator …
var score = 5;
score += 10;
console.log(score);
what would score be now?
15
Code showing how you can use variable names to add up values just as you would use numbers …
var score = 15;
var bonusPoints = 100;
var finalScore = score + bonusPoints;
document.write(finalScore);
115
What can one say about spacing around the = in JavaScript?
_ = _
How do you list strings?
Use double quotation marks “some string”.
How would you use html in a string?
If you’re using html in a string and have to use double quotes, use single quotes outside your string …
const htmlSnippet = ‘ < h1 class = “ headline “ > ‘
What is an escape character and how would you use it?
\ is the escape character and you use it before a character you want to use literally and not code-wise, i.e. …
const message = ‘ I'm a programmer ! ‘ ;
How to use an escape character when dealing with multi-line strings?
const multiLine = “ Hello , students \
Welcome to JS
Code to find the length of a string …
const passPhrase = “Open Sesame”;
console.log(passPhrase.length);
11
In JavaScript what is an action you can take called?
this one is called a method
Code for making a string all lower case …
const passPhrase = “I have spoken”;
console.log(“ passPhrase.toLowerCase());
>i have spoken
Code to list a string to all upper case …
const passPhrase = “i have spoken”;
console.log(passPhrase.toUpperCase());
>I HAVE SPOKEN
How to code for a question dialog box and to get a response to use somehow …
use the prompt() …
prompt(“What is your major malfunction?”);
How would you use the prompt to save some value …
const name = prompt(“What is your name?’);
console.log(name);
or
alert(name);
or
document.write( “ < h1 > name < / h1 > “ );
What is the process called when combining two or more strings together?
this one is called string concatenation
How to code to concatenate two string together?
“My favorite” + “Movies”;
My favorite Movies
How to code using a prompt and a string …
const name = prompt(“What is your name?”);
const message = “ Hello “ + name;
console.log(message);
or
alert(message);
or
document.write(message);
Code for using a template literal …
const name = prompt(“What is your name?”);
const message = Hello, ${name}
;
console.log(message);
What are some true “things” about template literals?
- With template literals you don’t have to worry about spaces or escape characters.
- With template literals you can write multi-line strings w/out any special characters, no back-slash for example.
How to display a value of a string onto a page …
It’s a two step process:
- Select the html element where you want your content to appear:
document. querySelector(‘main’); - What content you want to insert into that element.
Code using a querySelector …
const message = “What the fuck … over”;
document.querySelector(‘main’).innerHTML = message;
Code using some template literals using Great Gatsby …
const title = “ The Great Gatsby “;
const price = “ 14.99 “;
const book = ${title} : \$\${price}
;