Skip to content

Latest commit

 

History

History
101 lines (74 loc) · 2.44 KB

File metadata and controls

101 lines (74 loc) · 2.44 KB

16. More on Type

[toc]


16.1. Primitive Data Types

In JavaScript, ==data types can fall into one of two categories primitive or object types.==

==A primitive data type (PDT) is a basic buildinging block.** Using PDTs, we can build more complex data structures or object data types (ODTs).

While ODTs such as objects and arrays are mutable, PDTs are immutable. Remember, ==immutable data types are data types that cannot be cahnged once the value has been created.==

PDTs include:

  1. Strings
  2. Numbers
  3. Booleans
  4. undefined
  5. null

16.1.1. undefined

==undefined is a PDT in JavaScript which is assigned to declared varaibles which have not been initialized.==

let x;
console.log(x);		// undefined

16.1.2. null

==null is assigned to values that the programmer wishes to keep empty. It is similar to undefined in that it represents an unknown value, but the programmer defines it as such.==

let x = null;
console.log(x);		// null

16.1.3. Example

Let's say we are still working for the zoo. WE have objects created for animals like so:

let giraffe = {
   species: "Reticulated Giraffe",
   name: "Cynthia",
   weight: 1500,
   age: 15,
   diet: "leaves"
};

Now, a new giraffe is coming to the zoo. We may want to initalize an object for the giraffe, but hold off on storing how much the giraffe weights until we can weigh it on the scale when it gets here. In this case, we would initialize the weight property like so:

let giraffeTwo = {
   species: "Reticulated Giraffe",
   name: "Alicia",
   weight: null,
   age: 10,
   diet: "leaves"
};

This way our object is properly initilized with all the information we could need and we can update the weight property ler when we have accurate information.

16.1.4. Check Your Understanding ✅

Question: Which of the following are primative data types? Mark all that apply.

a. arrays b. Strings c. objects d. null

Answer: b. and d.

Question: Consider the following code block:

let x;

console.log(x);

x is of what type? a. null b. undefined c. NaN d. number

Answer: b. undefined

🏁 This one was short! Why can't more chapters be like this? Sadly these were exceptions to that. Speaking of exceptions, let's move on to Chapter 17.


#LaunchCode