Basics of JS Flashcards

1
Q

What are two ways to include JS to a file?

A
  1. Inline e.g between tags

2. Separate JS file and referring to it by link tags

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

Is JS client or server side?

A

Use to be only client side but can be server side if node.JS is used

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

What are the 3 most used web technologies used to make a website? What are their functions?

A

HTML - responsible for content
CSS - responsible for presentation of the content
JS - responsible for the functionality of the content

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

What are the five datatypes used in JS?

A

Numbers - float, decimals and integers
Booleans - logical data type e.g True or False
Strings - a sequence of characters
Undefined - Data type that doesn’t yet have a value
Null - No datatype

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

Why can’t you name a variable like:

var 3john/mark = 7;

A

In JS you cannot start a variable name with a number or symbol except for $ or _ . This will result in an unexpected token error.

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

What is type coercion in JS?

A

JS automatically converts datatypes to the necessary datatype needed to perform an action. E.g If you want to print a variable of type int to the console JS will turn it into a string first so that it can be printed to the console. This is called type coercion.

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

What is the ‘prompt’ keyword used for in JS?

A
The prompt keyword allows users input to be entered and stored. Has form:
var name = prompt('What is your name?');
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we create an alert that says ‘Wake up Jeff!’ when the web page is loaded?

A
var name = Jeff;
alert('Wake up ' + name);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the typeOf operator do?

A

The typeOf operator gives you the type of the variable. E.g typeOf 42 = int;

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

What is the precedence of ‘==’, ‘-‘, ‘+’ and ‘*’?
What is the order of operations in the following equation in JS?
4 + 6 * 3 - 7 = x;

A

Multiplication is highest with 15
Addition is tied second with subtraction both at 14
Last is equality with a precedence value of 11.
x = 15;

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

What symbols are used to add higher precedence?

Make 4 + 6*3 - 7 = 23

A

(4 + 6) * 3 - 7 = 23

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

Write a ternary operator to distinguish if someone is male or female based on chromosomes.

A

var sex = chromosomes == xy? ‘male’ : ‘female’;

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

What features must all switch statements have?

A
Each switch statement must have a controlling expression, a break and a case for each outcome. E.g
switch(job)
{
case 'teacher':
console.log(' is a teacher');
break;

case ‘boxer’:
console.log(‘ is a boxer’);
break;
}

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

What are the 5 falsy values?

A

They are undefined, null, 0, ‘ ‘, Nan

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

What is the difference between ‘===’ and ‘==’?

Which should we use more often and why?

A

’===’ is strict and does no type coercion
‘==’ uses type coercion
We should use ‘===’ more to avoid unexpected type errors.

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

Do function statements or function expressions return an immediate value?

A
Function expressions return an immediate value.
e.g var whatToDo = function(var 1, var2)
{ algorithms_to_be_executed; }
Function statements perform actions but don't produce immediate values e.g if, else, while/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Does the ‘shift’ keyword take element from the start or end of an array?

A

array_name.shift();

deletes a value from the start of an array

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

What does the indexOf keyword do and why is it useful?

A

array_name.indexOf(x) will return the position of element x in the array. It is useful for searching for variables in an array.

19
Q

What is the difference between objects and properties?

A

objects are unordered collections of related data that usually tell you about it.
Properties are the data that tells you data about an object or its behaviours. For example
Hex = {
firstName: ‘Hex’,
species: ‘Dog’,
isGoodBoi: ‘True’
};
Hex is the object
firstName, species, isGoodBoi are all properties.

20
Q

What does the ‘this’ keyword do?

A

The ‘this’ keyword is used in an object function to tell the computer that this function is to take place within the current object.

21
Q

How do loops help us work with arrays?

A

Loops can be used to iterate through an array until a variable is found or so that some sort of modification can be done on each element within it.

22
Q

How do you loop backwards through an array?

A

for(i = array.length; i === 0 ; i–)
{
do_this_code;
}

23
Q

What is the difference between the ‘continue’ and ‘break’ keywords?

A

‘Continue’ allows us to skip out of a loop before executing code within it and return to the start of the loop.
‘Break’ allows us to exit the loop and continue on further down outside of the loop.

24
Q

What are the 4 steps to solving any coding problems?

A
  1. Make sure you 100% understand the problem.
  2. Divide the problem into pieces and conquer each piece.
  3. Don’t be afraid to research.
  4. Write pseudo-code before starting.
25
Q

What are the 4 steps of debugging?

A
  1. Identify the bug.
  2. Find the bug using dev console or debugger.
  3. Fix the bug and replace error code.
  4. Prevent the bug happening again using specific user tests and checking for similar bugs throughout your code.
26
Q

What is the DRY principle?

A

DRY stands for Don’t Repeat Yourself. It refers to the process of replacing repeating code by creating functions that take care of a certain process.

27
Q

What is refactoring?

A

Refactoring is the process of changing code without changing the function of the code.

28
Q
What will the data type of x be in:
let x =='5'; ?

What can you do to make sure it is a string and not a number?

A

You can use the strict equality symbol (‘===’) not the loose equality symbol (‘==’).

29
Q

JS uses hoisting when running programs. What is hoisting and why is it used?

A

Hoisting is a practice that JS uses where it moves all the function declarations are moved to the top of the program when it is run. It is used to help with scope availability of variables.

30
Q

What is a JSON file and why are they useful?

A

A JSON file is a Javascript Object Notation file. It is useful for storing and transporting data when data is sent to a webpage.

31
Q

What are two examples of JSON functions?

A

JSON.stringify() - makes object into string.

JSON.parse() - makes string into object.

32
Q

How do we access an object property? E.g How would we access the property of ‘breed’ in object ‘dog’?

A

dog.breed;
To change the dog breed we would use:
dog.breed = ‘Husky’;

33
Q

If we want to reassign a number variable later in the code should we use the let, const or var keyword?

A

You should use the let keyword. ‘var’ will give you automatic global scope which uses up memory and ‘const’ will not let you reassign the value of a variable.

34
Q

Can you update the property of a object that has the modifier ‘const’ in front of it?

A

Yes you can. Const will not let you change the value of a variable but you can change the value of a property of an object that has the modifier const.

35
Q

If you get an undefined data type in a function what is likely to have happened?

A

If you get an undefined data type in a function it is likely because you have not returned a value in the function body.

36
Q

If you get an undefined data type in a method or statement what is likely to have happened?

A

If the value of a variable that is being evaluated or used doesn’t have an assigned value you will likely get an undefined data type.

37
Q

Why is using ‘ ’ in strings useful?

A

Using template literals (``) when in strings allows you to embed expressions and variables within the string. For example:
name === Jake;
control.log(Hi my name is ${name}); will give you:
Hi my name is Jake

38
Q

When should you use an object over an array?

A

You should use objects if you need to keep a bunch of varied data about one thing. You should use an array if you need to keep a list of similar data e.g student numbers?

39
Q

How would you display an array called ‘dogs’ as a table?

A

Using the console.table function. E.g

console.table(dogs);

40
Q

Why do we use arrow functions?

A

We use arrow functions as a short hand for writing functions introduced in ES6. It’s format is:

const double = (num) => {return num*2};

41
Q

array1 = [6, 10, 4];
What array would:
const array2 = array1.map(x => x*x) give us?

A

The map function allows us to create an array from the data manipulation of one array therefore,
array2 = [36, 100, 16];

42
Q

What keyword would we use to create an array using all the numbers greater than 10 from the array1 = [4, 11, 6, 5, 14, 30]?

A

The filter function creates an array using elements from the first array that pass a specified test. So
const array2 = array1.filter(number => number > 10);
the result:
array2 = [11, 14, 30];

43
Q

What is the shorthand to select all the elements in an array called ‘dog’?

A

We can use the spread syntax e.g

{…dog}