intro to Javascript Flashcards

(121 cards)

1
Q

let name = null:

TYPEOF//

A

object

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

3 reference types

A

objects
arrays
funtions

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

let colors = {“blue, “green’}

typeof colors

A

object

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

function greet (name){

console.log(“hello” + name)
}

name is called a ____

A

parameter

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

default value of variables

A

undefined

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

an EXPRESSION produces __

A

a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
let x = 10
console.log(++x)//

console.log(x)

A

11

11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
let x = 10
console.log(x++)//

console.log(x)

A

10

11

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

‘1’ === 1

A

false

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

strict operator compares _____ and ______

A

value and type

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

false || 1 || 2

A

1

operators go from left to right

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

two types of conditional statements

A

if…else

switch

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

when we need to repeat code we use ________

A

loops

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

name all the loops

A
for
while
do...while
for...in
for...of
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

let i = 0;

while (i <=10) {
  if (i % 2 === 0){
  i++;
 continue;
}

console.log(i);
i++;
}

RETURNS?

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

SHORTEN THIS

function isLandscape (width, height){
return (width > height) ? true : false;
}
A
function isLandscape (width, height){
return (width > height);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

instead of writing “Not a number” you can use ___

A

NaN

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

typeof NaN //

A

number

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

In JavaScript, any function can return a new object. When it’s not a constructor function or class, it’s called a __________

A

factory function.

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

IN MODERN JS WHAT CAN BE CHANGED?

function createCircle(radius){
 return {
    radius: radius, 
   draw: function (){
       console.log('draw')
       }
  };

}

A

make “radius: radius” to just “radius,:”

+++++++++

draw: (){
console.log(‘draw’)
}
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
function createCircle(radius){
 return {
    radius: radius, 
   draw: (){
       console.log('draw')
       }
  };
}

create a new circle with radius of 14

A

let circle1 = createCircle(14)

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

what is Pascal notation?

A

when the first letter of every word is capitalized used in creating constructor functions

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

in JS a factory function uses the keyword _____ to create an object

A

return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
function Circle(radius){
this.radius = radius;

}

ADD A DRAW FUNCTION

A

this.draw = function (){

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
when making a object from a constuctor function use the _____ keyword
new
26
when you use the new keyword with a constuctor function 3 things happen
1. JS creates empty object 2. sets "this" to point to new empty object 3. new operator return a this new object from the function "return this"
27
what uses "this" keyword 1. factory functions 2. constructor functions
constructor functions
28
what uses the "return" keyword to create a new object 1. factory functions 2. constructor functions
factory functions
29
when we use Const on a object we can not reassign the name TRUE / FALSE
TRUE
30
DELETE RADIUS ``` const circle = { radius: 1 } ``` console.log(circle)
delete circle.radius;
31
every object in JS has a property called _______
constuctor
32
a constructor references
the function that was used to construct an object.
33
``` function Circle(radius) { this.radius = radius; } ``` const another = new Circle(1) CREATE NEW OBJECT USING .CALL() METHOD
Circle.call({}, 1)
34
"THIS" REFERS TO WHAT AND WHY? ``` function Circle(radius) { this.radius = radius; } ``` const another = new Circle(1)
The object being created uses new keyword when creating new obect
35
"THIS" REFERS TO WHAT AND WHY? ``` function Circle(radius) { this.radius = radius; } ``` const another = Circle(1)
Global object which is window
36
``` function Circle(radius) { this.radius = radius; } ``` const another = new Circle(1) CREATE NEW OBJECT USING .CALL() METHOD POINTING TO ****WINDWO****
Circle.call(window, 1)
37
In JS functions are objects TRUE / FALSE
true
38
``` let x = 10 let y = x ``` x = 20 x? y?
20 | 10
39
``` let x = {value: 10}; let y = x; ``` x.value = 20; x? y?
{value: 20} {value: 20}
40
primitives are copied by their ______ | objects are copied by their ________
value | reference
41
_______ are copied by their value | _______ are copied by their reference
primitives objects
42
let number = 10; ``` function increase(number){ number++; } ``` increase(number); //? WHY?
10 we are dealing with let number at 10; not in scope of function as primitives copied by value
43
let obj = {value: 10}; ``` function increase(obj){ obj.value++; } ``` console.log(increase(obj)); //?
11 objects copied by reference
44
``` const circle = { radius: 1, color: 'blue' } ``` iterate to get keys
for (let key in circle) | console.log(key)
45
``` const circle = { radius: 1, color: 'blue' } ``` iterate to get values
for (let key in circle) | console.log(circle[key])
46
Object is or is not iterable
NOT
47
``` const circle = { radius: 1, color: 'blue' } ``` using FOR...OF iterate a OBJECT (even though for of is not the best way ) to get the keys
for (let key of Object.keys(circle))
48
const x = {value : 1}; this is a call to what function?
function Object() {}
49
``` const circle = { radius: 1, color: 'blue' } ``` using FOR...OF iterate a OBJECT (even though for of is not the best way ) to get the values
for (let key of Object.entires(circle))
50
``` const circle = { radius: 1, color: 'blue' } ``` CHECK TO SEE IF RADIUS IS IN CIRCLE
if ('radius' in circle) // code
51
an iterable ways to clone an object
for...in
52
for modern JS to copy an object is _______
object.assign()
53
a more modern way to doing this is ``` const another = {}; for (let key in obj) another[key] = circle[key] ```
const another = object.assign
54
clone circle using SPREAD operator ``` const circle = { radius: 1, color: 'blue' } ```
const obj = {...circle};
55
``` const circle = { radius: 1, } ``` const obj = USING OBJECT.ASSIGN CLONE OBJECT ADD COLOR
const obj = Object.assign({color: "yellow"}, circle)
56
_______ types don't have methods only objects | some examples
primitives string, number, boolean....
57
``` const message = "hi"; const another = new String('hi'); ``` console. log(typeof another); // console. log(typeof message); //
object string
58
``` const message = "hi"; there are methods on the above because JS creates a sting __________ ```
wrapper
59
``` let address1 = new Address ('a','b', 'c'); let address2 = new Address ('a','b', 'c'); let address3 = address1; ``` console. log(areEqual(address1, address3)); console. log(areEqual(address1, address2)); console. log(areSame(address1, address2)); console. log(areSame(address1, address3)); console. log(areSame(address2, address3)); ``` function Address (street, address, zip){ this.street = street; this.address = address; this.zip = zip; } ``` function areEqual(address1, address2){ return address1.street === address2.street && address1.address === address2.address && address1.zip === address2.zip } ``` function areSame(address1, address2){ return address1 === address2; ``` }
``` true true false true false ```
60
when you have nested loops, one way to clean up the code is too ________
break up the code in different functions and pass along the function
61
a another name for call back functions is a _________ function
predicate
62
Turn into a arrow funtion courses.find(function(course){ return course.name === 'a' });
courses.find(course=>course.name === 'a')
63
with an arrow function if you have no parameters you must use an
empty set of parathenthases
64
const numbers = [1,2,3,4]; 4 WAYS TO CLEAR ARRAY
numbers = []; numbers. length = 0; numbers. splice(0, numbers.length); while (numbers.length > 0) numbers.pop();
65
``` const numbers = [1,2,3,4]; let another = numbers number = [] ``` console. log(another); console. log(numbers);
[1,2,3,4] [1,2,3,4]
66
``` const first = [1,2,3]; const second =[4,5,6]; ``` ``` const third = first.concat(second); // write using spread operator ```
const third = [...first, ...second];
67
2 main ways to iterate an array
for...of | forEach()
68
what type of function uses a semicolon to end ?
function expression
69
``` let run = function (){ console.log("run") }; ``` // attach the varibable move to run run() move();
let move = run; DONT use () at the end. we are not calling the function.
70
What is hoisting in JS?
is the process of moving function declarations to the top of the program
71
``` function sum (a, b){ return a + b } ``` sum(1) // returns
NAN | 1 + undefined = NAN
72
the rest operator is associated with _____
function parameters
73
The spread operator is associated with ______
Arrays
74
``` function sum(...arg){ console.log(arg); }; ``` console. log(sum(1,2)); // ? console. log(sum(1,2,3)); // ?
array [1,2] array [1,2,3]
75
``` function sum(arg){ console.log(arg); }; ``` console. log(sum(1,2)); // ? console. log(sum(1,2,3)); // ?
1 1
76
``` function rates(price, years) { price = price || 3.5 years = years || 10 } ``` REWRITE TO DEFAULT VALUES
``` function rates(price = 3.5, years = 10) { } ```
77
``` function rate(principle, rates = 3.5, years) { return = principle + rates + years; } ``` rate(1000, 5) returns?
NAN JS does not know what to assign 5 too.
78
``` function rate(principle, rates = 3.5, years) { return = principle + rates + years; } ``` rate(1000, 5) returns an error. How to avoid
use "UNDEFINED" in the arguements
79
``` const person = { first: "steve" last: "me" fullName: function () {code} // WRITE SHORTER WAY } ```
fullName(){} drop function keyword inside an object in ES 6
80
We use ________ to access properties in an object
getters
81
we use _________ to change or mutate properties
setters
82
getters
to access properties in an object
83
setters
to change or mutate properties
84
in order to alter properties of an object from the outside we must have a __________
setter
85
When you have a getter in an object you can drop the _______ when you call it
the ()
86
defensive programing (try and catch) should be done when?
start of the function
87
to catch a error use the ______ constructor function
throw new error()
88
the ________ word is associated with the throw keyword
exception
89
the 3 keywords associted with checking a value
throw, catch, try
90
``` { const message = "hello"; } ``` console.log(message); // ?
error
91
``` { var message = "helloVar"; } ``` console.log(message);
helloVar
92
_____ and ______ are limited to the block they are defeined
let const
93
______ variables in a function take precedence over ______ varibales
local global
94
``` function loop (){ for (let i = 0; i < 5; i++){ console.log(i); } } ``` loop();
0 1 2 3 4
95
``` function loop (){ for (let i = 0; i < 5; i++){ } console.log(i); } ``` loop();
error i is let and outside scope
96
``` function loop (){ for (var i = 0; i < 5; i++) console.log(i); console.log(i); } ``` loop();
0 1 2 3 4 5
97
``` function loop (){ for (let i = 0; i < 5; i++) console.log(i); console.log(i); } ``` loop();
0 1 2 3 4 error let uses the line first code as a block
98
var scope is or is not limited to block it's defined but to the ______ its defined
NOT function
99
var creates block-scope or function scope variables?
function scope
100
let creates block-scope or function scope variables?
block-scope
101
const creates block-scope or function scope variables?
block-scope
102
``` function loop (){ for (var i = 0; i < 5; i++){ if (true){ var color = red; } } } ``` Is color accessible anywhere in the function?
yes because it's var and it uses function scope.
103
``` function loop (){ for (var i = 0; i < 5; i++){ if (true){ let color = red; } } } ``` Is color accessible anywhere in the function?
no because it's let and only uses block scope
104
``` var color = "blue"; let age = 44; ``` window. color// ? window. age // ?
blue - uses var and attached to window object | undefind -
105
``` function sayHi(){ code // } ``` TRUE / FALSE it the above attached to the window object?
true
106
What is "this" in JS?
the object that is executing the current function
107
if function a part of object called a method "this" references ____
the object itself
108
if function is not party of an object "this" references ____
the global object
109
``` const video = { title: "a", play (){ console.log(this); } } ``` video.play();
the video object | {title: "a", play: ƒ}
110
``` const video = { title: "a", play (){ console.log(this); } }; ``` video.stop = function (){ console.log(this); } video.stop();
the video object | {title: "a", play: ƒ, stop ƒ}
111
function playVideo(){ console.log(this); } playVideo();
the window object
112
``` function Video(title){ this.title = title; console.log(this); } ``` const v = new Video("a");
Video {title: "a"} constructor function creates new function and points this to object and returns object
113
``` const video = { title: "a", tags: ["a", "b", "c"], showtags (){ this.tags.forEach(function(tag){ console.log(tag); }); } }; ``` video.showtags(); // returns?
a, b, c
114
``` const video = { title: "a", tags: ["a", "b", "c"], showtags (){ this.tags.forEach(function(tag){ console.log(this); }); } }; ``` video.showtags(); // returns?
Window Object Window Object Window Object the callback function is a regular function and it's references global object (window)
115
``` function playVideo(){ console.log(this); } ``` playVideo.call({name: "me"}); playVideo();
// {name: "me"}; // window object
116
``` function playVideo(){ console.log(this); } ``` playVideo.call({name: "me"}); // playVideo.apply({name: "me"}); //
// {name: "me"}; // {name: "me"};
117
with the apply method the arguments must be passed at an ________ .
array
118
with the call method the arguments must be passed at an ________ .
sting with comma
119
Call, apply, bind. WHAT RETURNS a NEW FUNCTION?
Bind
120
``` function playVideo(){ console.log(this); } ``` playVideo.bind({name: "me"})(); //returns
// {name: "me"};
121
call(), apply (), bind() returns a function, which can be executed at a later time https://www.hacksparrow.com/javascript/bind-vs-apply-vs-call.html
bind()