Basics of JS Flashcards
What are two ways to include JS to a file?
- Inline e.g between tags
2. Separate JS file and referring to it by link tags
Is JS client or server side?
Use to be only client side but can be server side if node.JS is used
What are the 3 most used web technologies used to make a website? What are their functions?
HTML - responsible for content
CSS - responsible for presentation of the content
JS - responsible for the functionality of the content
What are the five datatypes used in JS?
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
Why can’t you name a variable like:
var 3john/mark = 7;
In JS you cannot start a variable name with a number or symbol except for $ or _ . This will result in an unexpected token error.
What is type coercion in JS?
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.
What is the ‘prompt’ keyword used for in JS?
The prompt keyword allows users input to be entered and stored. Has form: var name = prompt('What is your name?');
How do we create an alert that says ‘Wake up Jeff!’ when the web page is loaded?
var name = Jeff; alert('Wake up ' + name);
What does the typeOf operator do?
The typeOf operator gives you the type of the variable. E.g typeOf 42 = int;
What is the precedence of ‘==’, ‘-‘, ‘+’ and ‘*’?
What is the order of operations in the following equation in JS?
4 + 6 * 3 - 7 = x;
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;
What symbols are used to add higher precedence?
Make 4 + 6*3 - 7 = 23
(4 + 6) * 3 - 7 = 23
Write a ternary operator to distinguish if someone is male or female based on chromosomes.
var sex = chromosomes == xy? ‘male’ : ‘female’;
What features must all switch statements have?
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;
}
What are the 5 falsy values?
They are undefined, null, 0, ‘ ‘, Nan
What is the difference between ‘===’ and ‘==’?
Which should we use more often and why?
’===’ is strict and does no type coercion
‘==’ uses type coercion
We should use ‘===’ more to avoid unexpected type errors.
Do function statements or function expressions return an immediate value?
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/
Does the ‘shift’ keyword take element from the start or end of an array?
array_name.shift();
deletes a value from the start of an array
What does the indexOf keyword do and why is it useful?
array_name.indexOf(x) will return the position of element x in the array. It is useful for searching for variables in an array.
What is the difference between objects and properties?
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.
What does the ‘this’ keyword do?
The ‘this’ keyword is used in an object function to tell the computer that this function is to take place within the current object.
How do loops help us work with arrays?
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.
How do you loop backwards through an array?
for(i = array.length; i === 0 ; i–)
{
do_this_code;
}
What is the difference between the ‘continue’ and ‘break’ keywords?
‘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.
What are the 4 steps to solving any coding problems?
- Make sure you 100% understand the problem.
- Divide the problem into pieces and conquer each piece.
- Don’t be afraid to research.
- Write pseudo-code before starting.