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