Javascrip Code Camp Flashcards

1
Q

what does “immutable” mean when it comes to programming?

A

immutable - once they are created, they cannot be changed.
However, the variable assigned to the immutable value can still be reassigned another value.
Strings are immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

declaring a variable vs. initializing a variable

A

declaring a variable:
let greeting;
initializing a variable:
greeting = “Hello World”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

primitive vs. non-primitive data types

A

Non-primitive data types differ from primitive data types in that they can hold more complex data. Primitive data types like strings and numbers can only hold one value at a time.
An array is a non-primitive data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access something in an array?

A

nameOfArray[n]
where n is the index number of the item in the array.
Remember arrays always start with 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what function counts the number of items in an array?

A

.length
arrayName.length

By subtracting 1 from this ^^^, you get the index of the last element in the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Method vs. Function
1 distinction

A

A method in JavaScript is a function that’s associated with certain values or objects.
console.log
the .log() method, is part of the “console” object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

.push()

A

.push() method
“pushes” a value to the end of an array
It RETURNS the new length of the array, after adding the value you give it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

.pop()

A

.pop() method
It removes the last element from an array and RETURNS that element.
It really removes it. “Pops” it off the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

const vs. let
2 distinctions

A
  1. a const variable cannot be reassigned like a let variable. This code would throw an error:
    const firstName = “Naomi”;
    firstName = “Jessica”;
  2. A const variable cannot be uninitialized. This code would throw an error:
    const firstName;
    (you declared it, but didn’t initialize it).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

components of a for loop
4 components

A

for (iterator; condition; iteration statement) {
logic;
}
1. iterator - the thing (variable) you’re counting - you just declare it here.
2. condition - tells the loop how many times it should iterate. When the condition becomes false, the loop will stop.
3. Your iteration statement will tell your loop what to do with the iterator after each run.
4. Logic - what you want the program to do each time the loop runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does this do:
for (let i = 0; i < count; i = i + 1) {
console.log(i)
}

A

as long as i is less than the amount in the “count” variable, the value of I will be printed to the console and then 1 will be added to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does a for . . . of loop work in JavaScript

A

for (const row of rows) {
}
Where “row” is an item inside “rows” which is an array.
This type of loop iterates over each item in an iterable object (like an array) and temporarily assigns it to a variable.
sum=0
for (const score of scores) {
sum += score; (sum=sum+score)
}
this adds all the items in an array together

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is concatenation?

A

When you add a new string to an existing string.
let hello = “Hello”;
hello = hello + “ World”; you’re adding the string “World” to the existing string stored in the hello variable. This is called concatenation.
now hello = “Hello World”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

.repeat()

A

.repeat() method is used on strings.
accepts a number as an argument, specifying the number of times to repeat the target string.
For example:
const activity = “Code! “;
activity.repeat(3);

produces: “Code! Code! Code!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to create a function in JavaScript

A

function buba(parameter){
return “functions are cool!”
}
The function keyword tells JavaScript that “buba” is going to be a function. “parameter” represents a value/variable that is passed into the function when it is used.
A function may have as many, or as few, parameters as you’d like.
By default, functions return undefined as their value. In order to return something else, you need to use the return keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you call a function?

A

The syntax for a function call is the function name followed by parentheses. And inside the parentheses you put any necessary parameters.
For example,
padRow(8) – calls the padRow function and passes “8” as the parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is an “argument”

A

When you pass a value to a function call, that value is referred to as an argument - so when you pass a value as a parameter, that is an argument.
functionName(“bubba”);
In this case “bubba” is the argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Global Scope

A
  1. Where a variable is declared determines where in your code it can be used.
  2. Variables that are declared outside of any “block” like a function or
    loop are in the global scope.
  3. When a variable is in the global scope, you can use it in a functions logic. Or in other words, a function can access global variables so you can use global variables in function definitions.

const title = “Professor “;
function demo(name) {
return title + name;
}
Because “title” is a global variable, you can use it inside the function logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

local scope or block scope

A

Variables can also be declared inside a function. These variables are considered to be in the “local scope”, or “block scope”.
A variable declared inside a function can only be used inside that function. If you try to access it outside of the function, you get a reference error.

20
Q

What value inside a functions stops the execution of the function?

A

the return keyword
“return” not only defines a value to be returned from your function, it also stops the execution of your code inside a function or a block statement.
This means any code after a return statement will not run.

21
Q

What does PEMDAS stand for

A

Order of operations
Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction

22
Q

What does the += operator do?

A

It means itself plus whatever is next in the expression. For example, these two expression mean the same thing:
i = i + 1;
i += 1;

23
Q

what is the increment operator and how does it work?

A

++ is the increment opperator. It adds 1 to whatever variable you put it against.
bubba = 7;
bubba ++;
now bubba equals 8.

24
Q

what does “truthy” and “falsy” mean in JavaScript?

A

Truthy and Falsy values only occur when a non-boolean is evaluated as a boolean. For example, in an “if” statement, javascript is looking for a boolean value in the condition statement in between the parenthesis.

if (bubba = 2) {logic}
if (“farty”) {logic}

in the first example, the variable bubba will either equal 2 or it wont.
So you’ll get a clear boolean value.
In the second statement, “farty” is a string, which when evaluated to a boolean becomes true. This means “farty” (any non empty string) is a truthy value.

A truthy value is a value that is considered true when evaluated as a boolean. Most of the values you encounter in JavaScript will be truthy.

A falsy value is a value considered false when evaluated as a boolean. JavaScript has a defined list of falsy values. Some of them include false, 0, “”, null, undefined, and NaN. (The “” is an empty string.)

25
Q

How does an if else statement work?

A

if (condition1) {
// code to run if condition1 is true
} else if (condition2) {
// code to run if condition2 is true
} else if (condition3) {
// code to run if condition3 is true
}

If the first condition is false, JavaScript will check the next condition in the chain. If the second condition is false, JavaScript will check the third condition, and so on.

You can tack an “else” statement on at the end that will run if all the previous conditions were false

26
Q

what is a “while” loop and how does it work?

A

A while loop will run over and over again until the condition specified is no longer true. It has the following syntax:

Example Code:
while (condition is true) {
logic;
}

27
Q

compare and contrast == with ===

A

The equality operator == is used to check if two values are equal, but it doesn’t compare the type of data.

For example, “0” == 0 is true, even though one is a string and one is a number.

The strict equality operator === is used to check if two values are equal and share the same type.
With the strict equality operator, “0” === 0 becomes false, because, they do not have the same type.

** As a general rule, always use the strict equality operator.

28
Q

what does the “strict inequality” operator do?

A

The strict inequality operator !== allows you to check if two values are not equal, or do not have the same type

29
Q

what is -= and how does it work.

A

subtraction assignment operator
-=
subtracts the given value from the current variable value, then assigns the result back to the variable.

bubba = bubba - 2
bubba -= 2

These ^^^ mean the same thing.

30
Q

what is the decrement operator and how does it work?

A

When you are only subtracting one from a variable, you can use the decrement operator –.
bubba = bubba - 1
bubba –

These ^^^ mean the same thing.

31
Q

.unshift() method

A

The .unshift() method allows you to add a value to the beginning of an array.
.unshift() returns the new length of the array it was called on.

32
Q

How do you select items in the DOM?

A

The DOM is a tree of objects - You can access the HTML using the document object, which represents your entire HTML document.

One method for finding specific elements in your HTML is using the querySelector() method. The querySelector() method takes a CSS selector as an argument and returns the FIRST element that matches that selector. For example, to find the <h1> element in your HTML, you would write:

let h1 = document.querySelector(“h1”);

33
Q

id vs. class
# vs. .

A

id’s are specific and unique. You should only have 1 element per 1 id.
When referencing id’s you use #
so #bubbaGump would select the element with the bubbGump id

classes - there may be several elements that have the same class.
When referencing a class you use a period .
so .greenButton would select the class named greenButton

34
Q

what does “onclick” do? how do you use “onclick”

A

button.onclick = myFunction;

35
Q

what does innerText do? how do you use it?

A

innerText property controls the text that appears in an HTML element

The following changes the paragraph from “Demo content” to “Hello World”

<p>Demo content</p>

const info = document.querySelector(“#info”);
info.innerText = “Hello World”;

first you have to turn the id into a variable – using querySelector
THEN you can use the innerText property to change the text of an element

36
Q

what is an Object?

A

Objects are non primitive data types that store key-value pairs

{
key: value
}

This is how you make an empty object:
const objectName = {};

Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through properties.

Properties consist of a key and a value. The key is the name of the property, and the value is the data stored in the property.

const userProfile = {
name: “bubba”,
age: 5,
occupation: “pet”,
};

37
Q

can you have a space in the key aka property name of an object?

A

No. If the property name (key) of an object has a space in it, you will need to use single or double quotes around the name.
Example:

const spaceObj = {
“Space Name”: “Kirk”,
};

It will throw an error otherwise.

38
Q

What are the two ways to access the values inside an object

A

. dot notation
[] bracket notation

cat {
name: “whiskers”,
“Number of legs”: 4,
}

cat.name //whiskers
cat.[“Number of legs”] // 4

if the key has a space in it like “Number of legs” then you have to use bracket notation.

39
Q

can you have objects inside of arrays????

A

YES!

40
Q

.innerHTML vs. .innerText

A

InnerHTML: Using innerHTML allows you to see exactly what’s in the HTML markup contained within a string, including elements like spacing, line breaks and formatting. InnerText: This approximates the “rendered” text content of a node and is aware of styling and CSS

41
Q

what is a ternary operator?

A

The ternary operator is a conditional operator and can be used as a one-line if-else statement. The syntax is:

condition ? expressionIfTrue : expressionIfFalse

// traditional if-else statement

if (score > 0) {
return score
} else {
return default_score
}

// ternary operator that does the same thing

return score > 0 ? score : default_score

42
Q

what is this ||
and what does it do?

A

It’s the logical OR operator and it’s used when statements are evaluated.

with a statement like this:
num < 10 || num > 20

The logical OR operator will use the first value if it is truthy – that is, anything apart from NaN, null, undefined, 0, -0, 0n, “”, and false. Otherwise, it will use the second value.

seems like it’s kinda an mini if/else statement.

43
Q

what is &&
and what does it do?

A

It’s the logical AND operator
and it allows you to add a second condition to your if statement.

for example:
if (firstName === “Quincy” && lastName === “Larson”) {

}

44
Q

.includes()

A

The .includes() method determines if an ARRAY contains an element and will return either true or false.

const numbersArray = [1, 2, 3, 4, 5]
const bubba = 3

if (numbersArray.includes(bubba)) {
console.log(“The number 3 is in the array.”)
}

45
Q

What is a TypeError

A

A TypeError means that the code is trying to perform an operation on a value that is not of the expected type.

46
Q

when it comes to regex, what are shorthand character classes?

A

Shorthand character classes allow you to match specific characters without having to write those characters in your pattern.

Shorthand character classes are preceded with a backslash ().

For example: The character class \s will match any whitespace character. Add this to your regex pattern.

47
Q

.replace()

A

.replace() - replaces characters in a string with another string.
This method accepts two arguments.
The first argument is the character sequence to be replaced, which can be either a string or a regex pattern. The second argument is the string that replaces the matched sequence.
Since strings are immutable, the replace method returns a new string with the replaced characters.

Example Code
“hello”.replace(/l/g, “1”);&raquo_space; he11o

In this example, all instances of the letter l will be replaced with the number 1 in the string hello.
The /g after the L stands for “global” and tells the computer to keep looking for all L’s even after it finds the first one. w/o the g, it would stop looking after it found the first L.