javascript Flashcards
Does a variable have to be declared and assigned at the same time?
No, you can give it a name and then later in the code assign it a variable
var a;
(then later)
a = 7
what do we put at the end of our code to signify that it is the end?
;
What is the assignment operator?
=
what does the assignment operator do?
it assigns a value to a variable name
what does console.log( ) do?
allows you to see things in the console.
console.log(a);
this will return the value of (a) in the console
a = 7
can you use console.log ( ) to see what variables were at specific points in your program? how?
yes, you can. You can place console.log ( ) both before and after the change of value and it will print out the different values for each assignment
a = 7
console.log(a)
a = ‘happy’
console.log(a)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
7
‘happy’
what does a not defined string or name cause?
an error
what is the value set to a variable name that has not been assigned a value?
undefined
how can you increment number values by one?
variableName++;
how can you lower number values by one?
variableName–;
what are decimal numbers called?
floats
what is the operator to find the remainder of two numbers?
%
10 % 2;
= 0 (no remainder)
11 % 3;
= 2 (remainder)
what do we often use the remainder operator for?
to figure out if a a number is even or odd. If we can divide a number by 2 and it returns a remainder of 0, then it is even.
What shorthand can you use to add a variable name to a number? For instance, a = a + 7.
+=
a += 7
What shorthand can you use to subtract a variable name to a number? For instance, a = a - 7.
-=
a-=7
What is it called when you use the shorthand to apply an arithmetic operator to a variable name? (ex: a += 12)
Compound assignment
What do we need to remember about what happens to the variable name when using Compound Assignment?
the new value becomes assigned to the variable name. It doesn’t just compute it to find the answer.
(ex) a = 2 a + 2 = 4 console.log(a) 2
b = 6
b += 2 = 8
console.log(b)
b = 8
How do we use Compound Assignment with multiplication?
*=
How do we use Compound Assignment with division?
/=
what are three different things that can enclose a string?
single quotes ‘ ‘
double quotes “ “
backticks ` `
When we are using double quotation marks to enclose a string, how do we use double quotation marks inside of our string without the engine interpreting it as the end of the string?
You use the escape character \ before the inside “ - this tells the engine that they are just quotation marks and not the end of the string.
what is a literal string?
it’s a group of characters that are enclosed in single, double quotes pr backticks. The engine doesn’t interpret anything inside the sting until it receives a matching closing quotation mark.
When using single quotation marks to enclose a sting, how do we use single quotation marks inside of our string without the engine interpreting it as the end of the sting?
You use the \ escape character, in the same way you would with double quotes.
what if you use one type of quotes to enclose a literal string and another type of quotes inside the string?
Then there is no problem. You do not need to use an escape character.
Can we use backticks to enclose a string with single and double quotes inside them?
Yes we can
Recommended way to enclose a string?
with single quotes if you don’t a specific reason to use double or backticks
How do you escape a backslash in a string? And what does it do?
when you add a single backslash in a string it doesn’t show up when you render it. Escaping a backslash by using \ makes a single backslash appear.
What does the escape character \n inside of a string do?
it puts whatever that follows it inside the string on a new line.
(ex)
happy = ‘I want to \n be happy ‘
(result)
‘I want to
be happy’
what does the escape character \t inside of a string do?
it takes a tab space inside of the string
What is concatenating?
its adding two strings together end to end
string = “I need to start “ + “doing this regularly”
console.log(string)
“I need to start doing things regularly”
or we can do this
run = ' i want to do' walk = ' what i want to do '
run + walk = ‘ i want to do what i want to do ‘
What should we make sure we do when we are concatenating two or more strings as far as spacing?
we should make sure that there is space on the ends of the strings, so when it renders there is a space between them and looks normal and not smashed together.
“i want to “ + “go home” = ‘I want to go home’
‘i want to’ + ‘go home’ = ‘i want togo home’
Concatenating with Combined Assignment?
Just like you can add variable names to numerical values, to create new values for the variable names.
you can also do that with strings as well.
myStr = ‘ i went home’ += ‘ so i could sleep’
myStr = ‘ I went home so i could sleep’
How can we concatenate strings inside of other strings?
the same way we add other forms of strings, with the + operator.
myName = 'Rashaan' greeting = ' Hey how are you' + myName + ' How have you been?'
console.log(greeting)
“ Hey how are you Rashaan How have you been?”
How can we find the length of a string?
variableName.length
var name = ' rashaan' name.length
7
What does the number return on .length signify?
the number of characters in a string
what is indexing in a string?
its the built in function by the engine that assigns every character in a string a numerical value.
What is the first character in a string indexed as? (i.e. What does javascript start counting with?)
0.
rashaan
0123456
So the character length will come back as 7
but the actual count of the index will go up to 6 only because counting starts at 0
how do use Bracket Notation to find specific index of character in a string?
variableName[number}
the corresponding number will find the character that matches it and return it.
name = ‘rashaan’
name[0] r
name[3] h
name [6] n
What does immutable mean?
it means once something has been set, it is unable to be changed
Are the characters in strings mutable or immutable?
The characters in strings are immutable, they cannot be changed once they have been set.
(ex)
name = ‘danny’
name[0] = ‘m’
this will not result in ‘manny’ because characters inside of strings cannot be changed once they have been set. This will result in an error
Are whole strings immutable when assigned to variable name?
No, you can change the value to a variable name as much as you want.
name = ‘rashaan’
console.log(name)
name =’jenan’
console.log(name)
How do we use Bracket Notation to find the last character in a string? Especially if you don’t know how many letters there are in a string?
variableName [variableName.length -1]
name = ‘rashaan’
name[name.length - 1]
‘n’
What is ‘n’ or ‘nth’ number?
is it just a placeholder for an unidentified number or number value
How to use Bracket Notation to find the ‘nth - last’ character in a string?
You use the same syntax as finding the last character in a string, except you change the - 1 to a higher integer.
name = ‘rashaan’
name[name.length - 3]
‘a’
What is the value set to a string or a name that has not been declared to a keyword? (var, let, const etc.)
not defined
when using the additional operator to concatenate multiple parameters representing strings, what should we keep in mind as far as spacing?
We have to make sure that not only do we put spacing in between our strings inside of the opening and closing quotes, but we also need to make sure that we are putting spaces in between the parameter names. We do this using empty strings between the parameters.
result = “ The “ + adjective + noun + verb “ home”
console.log(result( ‘funny’, ‘boy’, ‘ran’));
The funnyboyran home
result = “ The “ + adjective + “ “ + noun +” “ + verb “ home”
console.log(result( ‘funny’, ‘boy’, ‘ran’));
When using multiple parameters in a function and then setting arguments to those parameters, what do we need to keep in mind when it comes to placement to ensure the proper order takes place in our desired code?
The order in which we put the arguments has to match the location of the parameter
function wordBlanks ( param 1 , param 2){ return param 1 + param 2} console.log(wordBlanks( "dog", 'house') console.log(wordBlanks( 'house, 'dog')
doghouse
housedog
What do arrays always start and end with?
a bracket
[ array ]
What do arrays always start and end with?
brackets
[ array ]
what are the elements inside of an array separated with?
a comma
[ element1, element2]
Can arrays contain elements of different data types inside of them/
yes
[ “Austin”, 3.16]
Can you use arrays inside of other arrays?
yes, arrays are allowed to contain arrays
What is a Nested Array?
It is an array that is inside of another array
nestedArray = [[“Giants”, 25], [49ers, 16]]
Can we use Bracket Notation in an array to index particular elements?
yes we can,
ourArray = [50, 60, 70]
ourArray[0]
returns 50
What is the difference between indexing a character in a string, and indexing an element in an array, as far as the value that is returned?
string indexing only gives the character
array indexing gives us the whole element and not just a specific element character.
string = ‘rashaan’
string[0] = ‘r’
array = [50, 60, 70]
array[0] = 50
How do we modify array data with indexes? Are arrays immutable?
No, they are not immutable. The way we couldn’t change a character in a string once it has been set, we can do in an array.
niners = [ 33, 43, 55] niners[2] = 16
niners = [33, 43, 16]
What are Multi-Dimensional Arrays?
Arrays with multiple arrays inside of them. They are an array of arrays
multi = [[1,2,3],[4,5,6],[7,8,9]]
How do we use Bracket Notation to index in Multi-Dimensional Arrays?
we use two seperate brackets. The first indexing the whole array, and then the second to specify the specific element
multi = [[1,2,3],[4,5,6],[7,8,9]]
multi[2][1];
8
multi[1][0];
4
How do we index a Nested Array inside of a Multi-Dimensional Array?
You add more bracket indexing to specify which element you want to greater detail.
var multi = [[1,2,3],[4,5,6],[7,[8,9]]]
multi[2][1][0];
8
How do we append a single or single elements to the end of arrays with push( )?
you add .push( ) to the name of the array and add what you want to be added to the end of the array inside the parens.
happy = [“john”, ‘dave’, ‘mike’];
happy.push(‘larry’);
happy;
[“john”, “dave”, “mike”, “larry”]
How do we append an array or arrays to the end of arrays with push( )?
add .push( ) to the name of the array and an array or arrays inside the parens with the included [ ]’s
sad = [‘sad’, ‘happy’, ‘meh’]
sad.push([‘content’,’hyped’])
sad;
[“sad”, “happy”, “meh”, ‘‘content”, “hyped”)]
How do we append an array or arrays to the end of arrays with push( )?
add .push( ) to the name of the array and add an array or arrays inside the parens with the included [ ]’s
sad = [‘sad’, ‘happy’, ‘meh’]
sad.push([‘content’,’hyped’])
sad;
[“sad”, “happy”, “meh”, ‘‘content”, “hyped”)]
What is the pop( ) function used for?
the pop function removes the last element from an array
arrayName[“austin”, 3.16]
arrayName.pop( )
arrayName;
[“austin”]
How do we remove an element from an array, and simultaneously assign it to a new variable name?
use a new variable name plus an assignment operator and the pop function in one line of code
arrayName = [‘austin’, 3.16, ‘says i just whooped your ass’];
var newVariableName = arrayName.pop( );
arrayName;
[“austin”, 3.16]
newVariableName;
“says i just whooped your ass”
How do we remove an array from an array using pop? And how do we assign it to a new variable name?
you use the same method that you would with a singular element
What is the shift( ) function?
it works almost exactly like pop( ). But it removes the first element of an array rather than the last element
What does the unshift ( ) function do?
unshift works exactly like the push function, except instead of adding an element or an array to the end of an array, it adds it to the front of an array
array = [‘dog’, ‘cat’, ‘wolf’];
array.shift( );
“dog”
array.unshift(‘happy’);
array;
(3)[“happy”, “cat”, “wolf”]
What are functions? What is their purpose?
to create reusable code
what does it mean to ‘call’ a function?
to call it to action, to run the action inside the curly brackets
how do you call a function?
use the name of the function followed by parens
nameFunc ( );
when you create a function, does it automatically run?
no, it will not run until you call it
in what way is a function repeatable when it doesn’t have any parameters?
It is repeatable but not modifiable, meaning you can run it over and over again without having to retype the code.
function test( ){ console.log('write this in console') }
test ( )
test ( )
test ( )
these three function calls of test ( ) would run the function code three separate times
in what way is a function repeatable when it has parameters?
it is repeatable and modifiable. Not only can you repeat the action that the function preforms, but you can modify the input and information that function renders or works with.
function newFunc(newStatement){ console.log(newStatement); }
newFunc(‘Hi my name is’)
newFunc(‘what?’)
newFunc(‘My name is’)
newFunc(‘who?’)
newFunc(‘chika chika Slim Shady’);
result:
Hi my name is what? My name is who? chika chika Slim Shady.
What is a parameter?
a parameter is the place holder in a function that will be modified later when the function is called
what is an argument?
an argument is the specified data that will replace the placeholder/parameter when the function is called
What does ‘scope’ refer to?
To the visibility of variables
variables that are defined outside of a function block have what type of scope?
Global Scope
What does Global Scope mean?
It means it can be seen anywhere in your JS code.
variables that are defined inside of a function using the var keyword has what type of scope?
Local scope.
The scope is tied to that function.
function funcName ( ){ var red = 'blue'; console.log(red);}
<b> later in code:</b>
red;
VM9788:1 Uncaught ReferenceError: red is not defined
console.log(red);
VM9836:1 Uncaught ReferenceError: red is not defined
<b> when running the function: </b>
funcName( );
blue
variables that are defined inside of a function without using a keyword get what type of scope?
Global scope.
function globalScope( ){ red = 'blue'; console.log(red); }
<b> The variable ‘red’ has the value of blue, not only when the function is ran, but when it is put anywhere in the code because it is global</b>
globalScope( );
blue
red;
“blue”
What is Local/Block Scope?
Local/Block Scope is when a variable and its value is tied to a function and parameters that it was defined in
Is it possible to have Global and Local/Block variables with the same name?
yes you can.
when you have a Global and Local/Block variable with the same name, which variable takes precedent when you call a function?
The local/block variable takes precedent.
<b> outside of function</b>
var outerWear = ‘Tshirt’;
<b> function </b>
function myOutfit( ) { var outerWear = 'sweater'; return outerWear; }
<b> Notice that the Local Scope variable only takes precedent when the function is called, and not over the variable name itself in all cases </b>
outerWear;
“Tshirt”
console.log(outerWear);
Tshirt
myOutfit( );
“sweater”
What does the return keyword do?
it creates a return statement in a function that will return a value when the function is ran.
function math(a){ return a - 5; }
console.log(math(10));
5
Do we need to add return statements to functions?
No we don’t have to
If we don’t have a return keyword in our function and don’t specify a value in our function what does it return?
undefined
How can we assign a define/declare a variable name to the value of a function?
variableName = functionName(argument);
<b> This makes the value of the function assigned to the variable ‘num’</b>
function inNum(a){ return a + 3; } num = inNum(10); 13
num;
13
<b> Notice that this doesn’t affect the future use of the function, the function is just a shell and hasn’t been run or called yet. You can still console log it to get a different value</b>
console.log(inNum(4));
7
undefined
<b> You can change the argument value and assign it to a different value as many times as you want</b>
dude = inNum(1400)
dude
1403
man = inNum(500)
man
503
What are the only two Boolean values?
True or False
What does an If statement do ?
it allows you to set one type of code to run if a statement is true and another one if it is false.
What is the syntax for an If statement?
function functionName { If ( conditional statement){ code ran if statement is true/false} )
var a = 22;
function tryIf( ){ if(a === 22) { return ' yeah its true'; } return 'no its all bad'; }
tryIf( );
“ yeah its true”
what does the == operator do?
it checks to see if the variable name contains the same value as the value supplied on the right side of the equality operator ?
x = 12; 12 x 12 x == 10 false
<b> It checks to see if they are equal regardless of data type (type conversion) </b>
3 == 3 true 3 == '3' true typeof 3 "number" typeof '3' "string"
what does the === operator do?
it checks to see if the contents of the left and right have not only the same value but also the same data type. <b>(doesn’t perform data conversion)</b>
3 === 3
true
3 === ‘3’;
false
typeof 3
“number”
typeof ‘3’
“string”
what is the name of the == operator ?
the equality operator
What is the name of the === operator ?
the strict equality operator
what is the name of the != operator?
the inequality operator
what does the inequality operator do?
it checks to see if both sides of the inequality operator are unequal regardless of data type. <b>( performs data conversion)</b>
if the sides are equal than the return is false, if they are unequal the return is true.
x=7;
7
y = 8;
8
x != y
true
x != x
false
<b> This also ignores data type, just like the equality operator. So, here we are looking for the return of false indicating that the number data type and the string data type are correctly being attributed the same value. A true statement means they aren’t the same value which doesn’t indicate that data type conversion is happening. </b>
3 != 3
false
3 != ‘3’
false
What is type conversion?
When one data type gets turned into another in order to perform its function
What is the name of the !== operator?
Strict Inequality Operator
What does the strict inequality do?
it does the exact same thing as the strict equality operator but it looks to see if the values are not equal to produce a true result - and if they are equal to produce a false
What is the greater than operator?
>
what is the greater than or equal to operator?
> =
How do we use a function to create and execute word blanks?
we use a simple function/parameter program :
function wordBlanks(noun, adjective, verb)
{ var result = 'The ' + adjective + " " + noun + " " + verb + ' all the way to the bank' return result;}
console.log(wordBlanks(‘fast’, ‘boy’, ‘laughed’));
The boy fast laughed all the way to the bank
What is the second thing we need to create a function?
The function name
function nameFunc
What is the third thing we need to create a function?
a set of parens after the function name
function nameFunc ( )
What is the first thing we need to create a function?
The keyword “function”
What is the 5th thing we need to create a function?
code inside the curly brackets, that will run anytime the function is called into action.
function nameFunc ( ) { console.log('write this in console') }
what is declaring a variable?
giving it a variable name
var myName
what is assigning a variable?
giving the variable name a value
myName = ‘rashaan’
What is the 4th thing we need to create a function?
curly brackets
function nameFunc ( ) { }
what is the lesser than operator?
what is the lesser than or equal to operator?
<=
How do we write the ‘And’ Logical Operator?
with double ampersands
What does the ‘And’ Logical Operator do?
It checks to see if both statements on either side of the operator are true. If one or both sides of the operator return false the whole statement will be false
How do we write the ‘OR’ Logical Operator?
||
What does the ‘OR’ Logical Operator do?
It checks to see if at least one of the expressions one either side of the logical operator are true, if at least one side of the operator is true than the whole expression is true.
x = 23 y = 55
x === 23 || y === 2;
true
How should we think about what a if statement is saying?
If this ….. do that
How should we think about what an Else If statement is saying?
And if also this…… then do that too
How should we think about an Else statement?
If none of that…… then just do this instead
what should we keep in mind in regards to order when we are running if/else if/else statements?
The code stops running at its first true, so the order is important.
function test(val){ if ( val < 10){ return 'less than ten'; } else if (val < 5){ return 'less than five'; } }
test (3);
“less than ten”
<b> Notice that the value that was returned was “less than ten” because 3 is less than 10 which is true, but its also less than 5 – but that result did not run… this is because once the first true is found, the code stops running. Its important that we write our code to get the most precise version of what we want in our code. </b>
function test(val){ if ( val < 10){ return 'less than five'; } else if (val < 5){ return 'less than ten'; } }
test(3);
“less than five
What can we use instead of chained if statements?
switch statements
What is a switch statement and what does it do?
it tests the value and has many case values that define many possible values
What is the syntax of a switch statement with 3 cases?
function learnSwitch(val){ var output = ""; switch(val){ case 1: output = 'a'; break; case 2: output = 'b'; break; case 3: output = 'c'; break; default: output = 'unfound'; break; } return output; }
learnSwitch(1);
“a”
learnSwitch(2);
“b”