JavaScript Flashcards

1
Q

Adding comments to your JavaScript

A

// for a single line comment

/* this is for a multiline comment */

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

How to declare value to a variable

A

var x = 5

here x is the variable and 5 is the value

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

Printing to the log

A

console.log()

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

The five primitive types

A

Number - Numberical values
String - Characters surrounded by single or double quotes
Boolean - true or false statements
Null - a variable that doesn’t have a value - this is delibrate!! You have to phyically name something null.
Undefined - a variable that is missing a value - NOT delibrate!

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

infinity and NaN

A

these are defined as numbers! not strings!!!

NaN means not a number lol

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

Some operating actions

A

+ Addition/Concatenation 5 + 4 9
- Subtraction 8 - 1 7
* Multiplication 6 * 7 42
% Modulu (remainder) 10 % 3 1
** Exponent 2**4 16

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

Adding numbers and Concatenating string

A
var x = 5
var y = 6
var z = "hello"
var a = "world"
console.log(x = y)
prints 11
console.log(z + a)
prings "helloworld"
console.log(z + " " + a)
prings "hello world"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

String Interpolation

A

Use backticks instead of quotes!!

var name = 'Tris'
var age = 7
var sentence = 'I'm ${name} and I'm ${age} !'
console.log(sentence)
prints "I'm Tris and I'm 7!"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Statements vs Expressions

A

Statements are instructions
var x = 1000 - this statment defines a value
console.log(‘hi’) - this statement logs a value
Expressions resolves to a value
x == y - expression that evaluates to false
15 + 2 - expression that evaluates to 17

(in a statment you’re telling the computer something. in an expression you’re asking the computer to answer something)

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

What is a Boolean Expression?

A

Expressions that resolve to a Boolean data type (true/false)

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

8 comparison operators

A

== Abstract equality (can compare things that don’t have the same primitive type
i.e. 1 == “1” where 1 is a number and “1” is a string)
!= Abstract inqueality (same as above but it’s telling you two things are NOT
equal
=== Strict equality (these HAVE to be the same primitive type)
!== Strict inqueality
< Less than
<= Less th an or equal to
> Greater than
>= Greater than or equal to

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

True or False? Anything that isn’t falsy is truthy

A

TRUE!!!!

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

True or False? NaN = NaN

A

FALSE! NaN means not a number. But they are not neccessarily the same NaN
Think of it like two differnt secrets
Or how a lion and a napkin are both not numbers but a lion isn’t equal to a napkin

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

if Statement example

A

if (boolean expression) {
code to run if expression is true
}

if (isLoggedIn) {
console.log(“User is logged in.”)
}

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

else if statement example

A
if (false) {
   This won't run.
} else if (true) {
   This will run.
}
if(userAge > 21) {
   console.log("User is an adult.")
} else if(userAge >12) {
   console.log("User is a teenager.")
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

else statement example

A
if(false) {
   This won't run
} else {
   This will run
}
if(isLoggedIn) {
   console.log("User is logged in.")
   // show profile page
} else {
   console.log("User is not loggedf in.")
   /// show sign in page
}
17
Q

switch statemnet example

A
switch(variable) {
   case value 1:
      code to run if variable is equal to value 1
   break
   case value 2
      code to run if variable is equal to value 2
   break
}

let currentPage = “home”

switch(currentPage) {
   case 'home':
      console.log('Display the homepage.')
//this will run
   break
   case 'profile'"
      console.log('Display the profile page.') 
//this will not run
   break
}
18
Q

switch default statement example

A

switch(variable) {
case value 1:
code to run if variable is equal to value 1
break
case value 2:
code to run if variable is equal to value 2
break
default:
code to run if variable doesn’t equal value 1 or 2
break
}

let currentPage = “foo”

switch(currentPage) {
   case 'home':
      console.log('Display the homepage.')
//this will not run
   break
   case 'profile':
      console.log('Display the profile page.')
//this will not run
   break
   default:
      console.log('Display a "page not found" error.')
//THIS WILL RUN! HOORAY
   break
}
19
Q

Array

A

a variable that stores multiple elements

20
Q

Concatenation

A

combining multiple strings

21
Q

Iteration

A

Repeating a process (lines of code)

Helpful for processing every element in an array

22
Q

What is a For Loop & The Anatomy of a For Loop

A

A way to iterate. Loops can run code a number of times

Initializer
Condition
Aferthought

for(let x = 0; x < 4; x++) {
      console.log(x)
}
23
Q

Initializer

A

Runs before the loop! Tells us where to start the loop

24
Q

Condition

A

Boolean expression that stops the loop when the answer is false. Without this, it would go on forever.

25
Afterthought
Runs at the end of each cycle in the loop. It tells the code how to move from the start of the loop to the next piece
26
Body (in forLoops)
The code to execute until the condition is false
27
``` let card = { number: 9 } ``` identify the object, the property, and the value
object: card property: number value: 9
28
Functions and their properties
``` a block of code designed to perfom task funtions must have - function name - pair of parentheses - pair of curly braces ```
29
Calling a function
To call a function, write only the name with a pair of parenthese ``` function sayHi ( ) { console.log("Hello!") } ``` sayHi ( ) is the function
30
Parameters
allow inputs to the function you can have as many as you want used to put new value inside parenthese when calling a function
31
Return Statements
the function stops executing and goes back to where the function was called
32
Scope | Global vs Local
The place in your code where a variable is defined Global: The scope that contains all other scopes; all variables defined here are visible in all contained scopes Local: An isolated scope typically defined by curly braces. All variables defined here are only visible within the scope itself
33
Dot notation
A way to access properties of an object using "."