Chapter 2 - Basic Javascript Instructions Flashcards
A statement is…
An individual instruction or step in a script e.g. var today = new Date();
Is Javascript case-sensitive?
Yes
Give a detailed description of a statement…
A statement is an individual instruction that the computer should follow.
Each one starts on a new line and ends with a semicolon
The semicolon lets the JS interpreter know when the statement is over, and that it should move on to the next step.
What is a code block?
A statement surrounded by a curly brace.
The closing curly brace is not followed by a semicolon
Code blocks will often be used to group together many statements.
This helps programmers organise their code and make it more readable.
Why write comments?
Comments help to explain what your code does
They help make your code easier to read and understand.
This can help you and others who ead your code.
How would you write a single-line comment?
A single line comment would be anything on a line that follows two forward slash characters
How would you write a multi-line comment?
You start with a /*
You end with a */
Anything between these two is not processed by the JS interpreter
When might you use a multi-line comment?
To describe how a script works
To stop a section of your script from running when testing it.
What is a variable?
A variable is a piece of information that a script needs to store temporarily so that it can do its job.
How would variables factor in the calculation of the area of wall?
You would need to multiply the width and the height of a wall to calculate its area
Variables would be used to temporarily store values for the height and width.
Which type of human memory might you compare variables to?
Short-term memory
If you have a script which can calculate the area of a wall, which part of this task is best represented by variables?
Variables are best used to represent the values in your script that are likely to change
In this case, it would be the specific heights and widths of each wall
Even though these change, the area of a wall will always be height x width.
What kind of data is generated by variables, when used together in a script?
Calculated or computed data.
How is a variable declared?
var is a keyword that the interpreter recognises and anticipates the creation of a variable
The variable must be given an identifier e.g. quantity in var quantity.
Give an example of how you would declare a variable?
var quantity;
How would you write a variable name of more than one word?
You would use camelCase
What does it mean to assign a value to a variable?
Telling a variable what information you want it to store for you.
What are some good practices when naming variables?
A variable name should describe the kind of data that the variable holds.
What is the significance of the equals (=) sign?
This is known as the assignment operator
It can assign or update the value given to a variable
If a variable is declared, but has now value assigned, it is said to be…
Undefined.
What are the three data types?
Numeric - integers, decimals and negatives
String - enclosed within single or double quotes - can contain HTML code
Boolean - True or false
What can numbers be used for?
Calculations
Determining screen size
Moving elements on the screen
Timing fading in of elements
What are the other data types?
Arrays
Objects
Undefined
Null
Do you need to specify a data type when declaring a variable in JavaScript?
No
Give and example of using a variable to store a number…
// Create three variables to store the information needed. var price; var quantity; var total;
// Assign values to the price and quantity variables. price = 5; quantity = 14; // Calculate the total by multiplying the price by quantity. total = price * quantity;
// Get the element with an id of cost. var el = document.getElementById('cost'); el.textContent = '$' + total;
How does variable assignment mirror algebra?
Once a variable has been assigned a value, you can use that variable’s name to represent that value.
Write an example of using a variable to store a string…
var username;
username = ‘Molly’;
Can you write strings on more than one line?
No.
Name two ways in which you can use quotes within a string?
If you want to use a double quote, surround the string in single quotes or vice versa.
Use a single backslash before the relevant quotation character. This is called escaping.
Give an example of using a variable to store a Boolean…
var inStock;
inStock = true;
What are two popular instances for the use of Booleans?
When the answer can only be true or false.
When your code can take more than one path.
Name four ways in which variables can be declared…
Variables declared and values assigned at the same time.
var price = 5;
Variables declared, then values assinged
var price, quantity, total;
price = 5; etc etc.
var price = 5, quantity = 14;
var total = price * quantity;
Variable is used to hold a reference to an to an HTML element
This allows you to work directly with the element stored in that variable.
var el = document.getElementById(‘cost’);
el.textContent = ‘$’ + total;
How might you change the value of a variable?
var inStock = true; inStock = false;
What are the six rules for naming variables?
The name must begin with a letter, a dollar sign or an underscore.
Names can contain letters, numbers , dollar sign and underscore.
Names must not use keywords or reserved words
Variables are case-sensitive. Don’t create two with the same name in different cases.
Use a name that describes the type of information the variable stores.
If your variable name is more than one word, use camelCase.
What is an array?
A special type of variable used when with a list or a set of values related to eachother.
When using an array you do not need to know or specify how many values it will hold.
How would you create an array?
ARRAY LITERAL
var colors;
colors = [‘white, ‘black’, ‘custom’];
ARRAY CONSTRUCTOR var colors = new Array(‘white’, ‘black’, ‘custom’);
Which method might you use to retrieve data from an array?
item() - index number of the item goes in the parenthese.
What is the standard first index value of the first item in an array?
zero (0)
How would you access an item in an array?
var itemThree;
itemThree = colors[2];
How would you find the length of an array?
var numColors;
numColors = colors.length;
How would you change a value in an array?
colors[2] = ‘beige’;`
What is an expression?
Something that evaluates into a single value
What are the two main types of expression?
Expressions that assign a value to a variable. var color = ‘beige’;
Expressions that use two or more values to return a single value var area = 3 * 2;
What types or operator are commonly used in JavaScript?
Assignment Operators - color = ‘beige’;
Arithmetic Operators - area = 3*2;
String Operators - greeting = ‘Hi’ + ‘Molly’;
Comparison Operators - buy = 3 > 5;
Logical Operators - buy = (5 > 3) && (2
What are the types of arithmetic operators?
Addition + Subtraction - Division / Multiplication * Increment ++ Decrement -- Modulus %
In what order are arithmetical operations executed?
Multiplication and Division before Subtraction and Addition
Brackets can be used to overcome this.
Give an example of using Arithmetic Operators…
// Create a variable for the subtotal and make a calculation var subtotal = (13 + 1) * 5; // Subtotal is 70
// Create a variable for the shipping and make a calculation var shipping = 0.5 * (13 + 1); // Shipping is 7
// Create the total by combining the subtotal and shipping values var total = subtotal + shipping; // Total is 77
// Write the results to the screen var elSub = document.getElementById('subtotal'); elSub.textContent = subtotal;
var elShip = document.getElementById('shipping'); elShip.textContent = shipping;
var elTotal = document.getElementById('total'); elTotal.textContent = total;
// Note: textContent does not work in IE8 or earlier - see explanation on website
Joining two or more strings together is called…
Concatenation
What do you need to remember when mixing numbers and strings?
When you place quotes around a number it becomes a string. It will be concatenated, not added.
When you add numeric data to a string, it becomes part of the string.
If you try to use any arithmetic operators on string apart from +, the result is NaN.
Give an example of using String Operators
// Store the greeting in a variable var greeting = 'Howdy ';
// Store the users name in a variable var name = 'Molly';
/* Create the welcome message by concatenating the strings in the two variables */ var welcomeMessage = greeting + name + '!';
// Get the element that has an id of greeting var el = document.getElementById('greeting');
// Replace the content of this element with the personal message el.textContent = welcomeMessage;
// Note: textContent does not work in IE8 or earlier - see explanation on website
Give an example of Basic JavaScript Instructions.
// Create variables for the welcome message var greeting = 'Howdy '; var name = 'Molly'; var message = ', please check your order:'; // Concatenate the three variables above to create the welcome message var welcome = greeting + name + message;
// Create variables to hold details about the sign var sign = 'Montague House'; var tiles = sign.length; var subTotal = tiles * 5; var shipping = 7; var grandTotal = subTotal + shipping;
// Get the element that has an id of greeting var el = document.getElementById('greeting'); // Replace the content of that element with the personalized welcome message el.textContent = welcome;
// Get the element that has an id of userSign then update its contents var elSign = document.getElementById('userSign'); elSign.textContent = sign;
// Get the element that has an id of tiles then update its contents var elTiles = document.getElementById('tiles'); elTiles.textContent = tiles;
// Get the element that has an id of subTotal then update its contents var elSubTotal = document.getElementById('subTotal'); elSubTotal.textContent = '$' + subTotal;
// Get the element that has an id of shipping then update its contents var elShipping = document.getElementById('shipping'); elShipping.textContent = '$' + shipping;
// Get the element that has an id of grandTotal then update its contents var elGrandTotal = document.getElementById('grandTotal'); elGrandTotal.textContent = '$' + grandTotal;
// Note: textContent does not work in IE8 or earlier - see explanation on website