Data Types Flashcards
What is a primitive data type?
a basic data type that is not an object and has no methods or properties of its own
Name the six primitive data types. S,N,B,U,N,S
String, Number, Boolean, Undefined, Null, Symbol
What does null mean?
The absence of a value. A deliberate non-value, a variable intentionally empty.
What is undefined?
A variable that has not been assigned a value or a function that does not return anything.
Give an example of an undefined variable
let name;
Give an example of a null variable
let n = null;
Give three examples of when you might want to return a null value
1) When initialising a variable that does not yet have a a value.
let username = null;
2) Clearing the value of a variable. username = null;
3) If a function cannot return a value - i.e no user id in database, then option to return ‘null’.
function getUser(id) {
if (id === 0) {
return { name: ‘John’ };
} else {
return null;
}
}
What code can we write in console.log to find out the type of a variable? e.g let age = 5;
console.log*typeof age) - with the space.
What is an object?
data type that represents a collection of related data or functionality. An object can be thought of as a container for properties and methods
How can we create an object without any properties or values yet?
let emptyObj = new Object();
What is an object ‘literal’ from this code let person = { name: ‘John’, age: 30 };
{ name: ‘John’, age: 30 };
It is called a literal because it is a notation that represents the properties and values of the object directly in the code, as opposed to creating an object using a constructor function.
Create an empty object using an object literal
let emObj = {};
let person = { name: ‘John’, age: 30 }; How can I access age through dot notation?
Console.log(person.age);
What is a primitive value vs reference value?
const num1 = 6;
const num2 = num1;
Arrays - let shoppingItems = [“apple”, “dogFood”, “milk”];
What method can I use to add a new item onto the array? i.e “Oranges”
shoppingItems.push(“Oranges”);
MEMORY AID = Imagine furiously pushing an array that it makes the last one birth a baby value.