Chapter 2 - Basic Javascript Instructions Flashcards
What is a script?
A script is a series of instructions that a computer can follow one-by-one
Each individual instruction in a script is also known as?
A statement - an individual instruction that a computer should follow. Each one should start on a new line and end with a semi-colon. This makes your code easier to read & follow
Each of the statements or instructions in a script should be followed by a
semicolon
what do the pink curly braces in scripts represent?
they represent the start and end of a code block. statements can be organized into code blocks. the closing curly braces isn’t followed by a semi-colon
each code block could contain more than 1 statement - true or false?
true
Is javascript case-sensitive
yes
What are some of the reasons why you should comment in your code.
What are the methods that can be used to comment in a javascript code
to explain what your code does. they help your code easier to read & understand. this can help you & others who read your code
multi-line and single-line
How to write multi-line comments
to write a comment that stretches over more than one line… /…./. Anything between these characters is not processed by the Javascript character… they are often used for descriptions of how the script works, or to prevent a section of the script from running when testing it
how to write a single-line comment
anything that follows the two forward slash characters //….. often used for short descriptions of what the code is doing
What is a variable?
A script will have to temporarily store the bits of info it needs to do its job. It can store this data in variables.
What is the #1 thing to do when using a variable?
declare the variable: announce that you want to use it - create the variable and give it a name
how to declare a variable
var name;
var = variable keyword name = variable name (in order to use a variable, you must give it a name or an identifier. if a variable name is more than one word, it is usually written in camelcase - first word is all lower case and any subsequent words have their first letter capitalized)
how to assign values to variables?
once you’ve created a variable, you can tell it what info you would like it to store for you - you assign a value to the variable i.e. var quantity = 3; … you can now use the variable by its name. the equal sign is an assignment operator, it says you’re going to assign a value to the variable… until you’ve assigned a value to a variable, programmers say the value is undefined.
What are the javascript Data Types?
- Numeric Data Type: you’ll use numbers 0-9, no commas. Numbers aren’t only used for things like calculators, they’re also used for tasks such as determining the size of the screen, moving the position of an element on a page, or setting the amount of time an element should take to fade in.
- String Data Type: Consists of letters and characters. Enclosed between a pair of quotes (can be single or double but must match) - they’re frequently used to add new content into a page and they can contain html markup.
- Boolean Data Type: Can have one or two values: true or false. Booleans are helpful when determining which part of a script should run. Booleans are used when the value can only be true/false. You could also think of these values as on/off or 0/1: true is equivalent to on or 1, false is equivalent to off or 0. Also, Booleans are used when your code can take more than one path. Remember, different code may run in different circumstances
- Arrays
- Objects
- Undefined
- Null
What are some shorthand techniques for creating variables?
- Variables are declared and values assigned in the same statement.
- 3 variables are declared on the same line, then values assigned to each of them
- 2 variables are declared and assigned values on the same line. Then one is declared and assigned a value on the next line.
- Here, a variable is used to hold a reference to an element in the html page. This allows you to work differently with the element stored in that variable
What are rules for naming variables?
- The name must begin w/ a letter, dollar sign, or an underscore (_). It must not start with a number
- you must not use a dash (-) or a period (.) in a variable name
- no keywords or reserve words. keywords are special words that tell the interpreter to do something. Reserved words are ones that may be used in a future version of JavaScript (refer to text)
- all variables are case sensitive, so score & Score would be different variable names, but it’s bad practice to create two variables that have the same name using different cases
- Use a name that describes the kind of info that the variable stores i.e. firstName for someones first name
- If your variance name is made up of more than one word, use a capital letter for the first letter of every word after the first word i.e. firstName
What are arrays?
A special type of variable. It doesn’t just store one value; it stores a list of values. Consider using an array whenever you’re working w/ a list or a set of values that are related to each other; also, when you don’t know how many items a list will contain b/c when you create the array, you don’t need to specify how many values it’ll hold. If you don’t know how many items a list contain, rather than creating a long list of variables, use an array. Values in an array don’t have to be the same data
What ways can you create an array?
array literal - the preferred method… by writing each value on a separate line
array constructor - uses the “new” keyword followed by Array() - the values are then specified in the parenthesis and each separated by a comma.
What is an Expression?
Evaluates into (results in) a single value. Broadly speaking there are two types of expressions. Expressions rely on 2 things called operators; they allow programmers to create a single value from one or more values
What are the types of expression?
- Expressions that assign a value to a variable:
- Expressions that use two or more values to return a single value: you can perform operations on any number of individual values to determine a single value.
List some of the arithmetic operators?
Addition + (Adds one value to another)
Subtraction - (Subtracts one value from another)
Division / (Divides 2 values)
Multiplication * (multiplies 2 values using an asterisk)
Increment ++ (adds one to the current number)
Decrement – (subtracts one from the current number)
Modulus % (divides 2 values and returns the remainder)
What is the String Operator?
There is just one string operator: the + symbol. It is used to join the strings on either side of it - for situations where you may need to join two or more strings together to create one new string (concatenation)