Chapter 2: Basic Javascript Instructions Flashcards
Statements
An individual instruction or step in javascript. Ends with a semi colon
var today = new Date();
Comments
Used to explain code. You can have multi line using syntax /* */ or single line using // syntax . Any script surrounded by a comment is not processed by the js interpreter
Is Javascript a case sensitive language?
Yes. hournow and hourNow would be treated as two different items
code block
lines of code surrounded by curly braces { } do not end in a semi colon
Variable
variables are used to store bits of information. If you need to reuse a piece of information you’re best to store it in a variable. Programmers say you declare a variable. You can then assign it a value. Variables stand in for a value, so rather than saying the specific number it represents (which may change), its use whatever is stored in this item instead
var dinosaur = “Spinosaurus”;
=
This is the assignment operator. it assigns a value to a variable.
undefined
Until a variable has a value its undefined
Data types
Javascript variables can have a range of types.
numeric (e.g. 2, 0.75)
String (“Used for text)
Boolean (results in a true or false value)
There’s also arrays, objects, undefined and null
Quote marks
Used to mark out strings. You can use single or double but they must match i.e. don’t start with string ‘ and end with “
escaping quotes
Used when you want to show quote marks inside a string.
You can either use a backslash before a quote mark or Start the string with double quotes and then use single quotes inside the string
Booleans are useful…
- When a value can only be true or false
2. When code can take more than a single path (allows us to test if a condition/s are true or false)
Rules for naming variables
- must begin with a letter, dollar sign or underscore
- names can contain letters, numbers, $ or an underscore
- You cannot use keywords/reserved words
- case sensitive
- use a name that describes the info that the variable stores
- camelcase
Array
Arrays store a list of values or related values. The values are indexed by a number starting at 0.
Using an array is useful instead of creating individual variables when you are unsure of the number of items
var colours = [‘white’, ‘black’, ‘red’];
array literal syntax vs array constructor sytax
literal: var colours = ['white', 'black', 'red'];
constructor: var colours = new Array ('white', 'black', 'red');
Access values in an array
by index number
var colours = [‘white’, ‘black’, ‘red’];
colours[2];
by item
var colours = [‘white’, ‘black’, ‘red’];
colours.item(1);