Data types Continued

(Note: This is a continuation of the last article)

Previously we looked at how variables in JavaScript can fall into a list of certain types.
Some of these types fall into a catagrory known as primitive types. This means that everything in JavaScript is one of these types.

string, number, boolean, undefined, null, symbol, bigint

All of these we have covered except the last three which you won’t need to worry about. `null` usually occurs indirectly when you try to use a variable that does not exist. `symbol` and `bigint` are rarely used because of their narrow use case and people’s inexperience with them. Do not worry about them.

You may have noticed that some types were missing such as `array`. This is because everything else in JavaScript is a special type known as an object.

To create an object we use curly brackets and by convention use the `const` keyword.

const myObj = {}

They can store any type including more objects. Objects are also known as key value pairs, because of how they are defined. Ech pair is defined by `key: value`, where `key` is a string and `value` is any type.

`key` it can be any value but JS turns it into a string.
`key` has to be a literal value, ie not a variable.
`key` can be defined without quotes.

const user = {
    "full name": "John Doe",
    age: 34,
    hobbies: ["Painting", "Coding", "Running"]

In an object we can change a specific item either by using `KEY.VALUE` or `KEY[“VALUE”]`. We mainly use the first option but are forced to use the second when VALUE is not a valid varaible name (has spaces, starts with a number, etc) or VALUE is a variable (x = “time”; myobj[x])

user["full name"] = "Jane Doe"
user.age = 32
user.hobbies[2] = "Sleeping"

The end

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.