JS Basics/ES5 Flashcards
How many data types are there in JavaScript?
Name them.
8:
undefined
null
boolean
string
symbol
bignit
number
object
How is a variable declared in JS? Give an example.
We declare a variable by putting the keyword var in front of it:
var myName;
How are statements ended in JS?
With a semicolon (;).
What are the rules for variables’ naming?
Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with a number.
What is the diffence between declaring and initialising a variable? What is the quickest way to do both?
Declaring is creating (naming) a variable. Initialising in assigning a value to the variable for the first time.
The quickest way to do both is to do them in one line:
var myName = “anti”;
or
var myNumber = 7
What is a string literal?
A series of zero or more characters enclosed in single or double quotes.
What is the default value of an declared but not initialised variable?
What message do you get when you try to use a mathematical operation involving such a variable?
What about string concatenation?
undefined
for mathematical: NaN (Not a Number)
for concatenation: undefined
Is JS case sensitive?
Yes.
What is best practice in naming variables?
Give an example.
Write variable names in JavaScript in camelCase. In camelCase, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized.
Example:
var someVariable;
What is the difference between var and let?
When a variable is declared using the var keyword, it can be re-declared without throwing an error; but when the let keyword is used, each variable must have unique names i.e. trying to re-declare it with the same name will thow an error.
How is a read-only variable declared?
Give an example.
Use the const keyword.
Example:
const myReadOnlyVariable;
Compare the kewords const and let in JS.
const has all the features of let but it is read-only.
What is best practice in naming constants (variables that are not supposed to be changed)?
Should be all upper case.
What are two other names for decimal numbers in JS?
Floating point numbers or just floats
In JS, what is the remainde operation symbol? What does it do?
%
It returns the remainder of a division: e.g. 5%2 = 1
What is the most common usage of the remainder operation?
To find out if a number is even or odd. If the remainder is zero then the number is even.
Comment on remainder vs modulus.
They are not the same.
The remainder does not work well with negative numbers.
In programming, which side of an operation in evaluated first?
The right side of the assignment.
What do the += and -= operators do?
What about *= and /=?
They add or subtract what’s on the right-hand side to or from the variable on the left-hand side of the operator, respectively.
The others do the same but with their respective operations.
E.g. myVar *= 3; is the same as myVar = myVar * 3;
In JS, how can you place quotes inside quotes?
By using the escape () symbol before the inner quotes.
Comment on the use of single vs double quotes in JS.
They can both be used to create string literals as long as the string starts and ends with the same type of quote.
What are the 8 most common uses of the escape character?
' - single quote
" - double quote
\ - backslash
\n - new line
\t - tab
\r - carriage return
\b - word boundary
\f - form feed
In JS, when you use a + with Strings, what is it called?
Concatenation
Can we use += and *= in concatenation?
Yes.
In JS, how can we find out how long a strig is?
By appending .length to the string literal or to a variable where the string literal is stored.
E.g.
console.log(myVar.lenght);
What is Zero-based indexing?
It’s the property of programming languages that dictates that the code starts counting at 0 rather than 1.
What is the bracket notation?
Give an example.
It is a way to get a character at a specific index within a string.
E.g. If const firstName = “Charles”, you can get the value of the first letter of the string by using firstName[0].
const firstName = “Charles”;
const firstLetter = firstName[0];
console.log(firstLetter) would return C
In JS, what is string Immutability?
The property that dictates that a single character in a string literal cannot be changed.
The only way to change a string literal is to assign a new string to the variable stroring it.
let myStr = “Bob”;
myStr[0] = “J”;
The code above would return an error.
The code below is the only way to do it:
let myStr = “Bob”;
myStr = “Job”;
How can you get the last character of a string using bracket notation and the .length method?
const firstName = “Anti”;
const lastLetter = firstName[firstName.length - 1];
This would assign the letter “i” to lastLetter.
How can you use bracket notation to find the nth-to-last character in a string?
const firstName = “Augusta”;
const thirdToLastLetter = firstName[firstName.length - n];
If n=3, then thirdToLastLetter= “s”.
What is the below line of code:
const sandwich = [“peanut butter”, 55 , “bread”, 45.6, myVar];
The declaration of an array.
What is the code line below?
const teams = [[“Bulls”, 23], [“White Sox”, 45]];
A multidimetional array aka nested arrays i.e. arrays as elements of an array.
How is array data accessed?
Using indexing, just like with strings.
const array = [50, 60, 70];
array[0]; //50
const data = array[1]; //60
Are elements of an array mutable?
Yes, at will; even if the array is declared as a const.
const ourArray = [50, 40, 30];
ourArray[0] = 15; //50 is replaced by 15
Comment on the below:
array [0];
There shouldn’t be any spaces between the array name and the square brackets, like array [0]. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
What would the below return?
const arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], 13, 14] ];
arr[3][0][1];
What is it called?
11
Multidimetional indexing.
In JS, what does the .push() method do in relation to arrays?
Visualise examples.
It adds specified parameters at the end of an array.
const arr1 = [1, 2, 3];
arr1.push(“sam”); //[1, 2, 3, “sam”]
or
const arr2 = [“Stimpson”, “J”, “cat”];
arr2.push([“happy”, 6]); //[“Stimpson”, “J”, “cat”, “happy”, 6]
In JS, what does the .pop() method do in relation to arrays?
Visualise an example.
It removes the last parameter of an array.
const threeArr = [1, 4, 6];
const oneDown = threeArr.pop();
console. log(oneDown); //6
console. log(threeArr); //[1, 4]
In JS, what does the .shift() method do in relation to arrays?
Visualise an example.
It does the same as .pop() but removes the first element.
const ourArray = [“Stimpson”, “J”, [“cat”]];
const removedFromOurArray = ourArray.shift();
console. log(removedFromOurArray); //[“Stimpson”]
console. log(ourArray); //[“J”, [“cat”]]
In JS, what does the .unshift() method do in relation to arrays?
Visualise an example.
It does the same as .push() but adds parameters to the beggining of the array.
const ourArray = [“Stimpson”, “J”, “cat”];
ourArray.shift(); //[“J”, “cat”]
ourArray.unshift(“Happy”); //[“Happy”, “J”, “cat”]
In JS, what is a function?
Visualise an example of how to declare a function.
It is a way to reuse code by calling a defined function’s name.
A function is declared as below:
function functionName() {
console.log(“Hello World”);
};
Each time the funtionName() function is invoked, a message of Hello World will appear in the console.
In JS functions, what are parameters?
Visualise an example.
Parameters are variables that act as placeholders for the values that are to be input to a function when it is called.
When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or “passed”) into a function when it is called are known as arguments.
function testFun(param1, param2) {
console.log(param1, param2);
}
Then we can call testFun like this: testFun(“Hello”, “World”);. We have passed two string arguments, Hello and World. Inside the function, param1 will equal the string Hello and param2 will equal the string World. Note that you could call testFun again with different arguments and the parameters would take on the value of the new arguments.
In JS, what does the return statement do?
Visualise an example.
We can pass values into a function with arguments. Conversely, you can use this statement to send a value back out of a function.
function plusThree(num) {
return num + 3;
}
const answer = plusThree(5); // answer will be 8 becasuse plusThree(5) has a value of 8.