Javascript Bootcamp | Basics | Part 2–9
Mastering the Basics
Intro
Welcome back, gang! We are finally going to start getting into the nitty-gritty of JavaScript. Here are my notes repurposed in a way that should enlighten you on the foundation of Javascript. Data structures and objects.
I will be adding videos to supplement these commentaries. I will be recording three at a time weekly. And editing them for uploads. Until then, enjoy these articles, please! Do not forget to follow and clap! I will be going over a large range of topics as these are the topics that I am interested in or studying at uni. These are essentially public notes and a way to reinforce the info for myself. However, I aim to create these notes in an informative and entertaining way for you to use in your learning process as well. Good luck!
03 Data Structures & Objects
Data structures are the letters and words of the novel that is your application. These constituent elements are the foundational components that make up the app itself and is what JS is largely built on.
Type vs Non-Type
JS is a dynamically/weakly typed language. as opposed to python which is a statically typed language. All this means is that in js we are more flexible in how we call and assign variables vs other languages. Let's explore what I mean:
In python, you are usually assigning the variable with the type of variable it is.
int num = 5 // integer
str word = "hello" // string
bool active = true // boolean
whereas in javascript we won't get any parse errors if we assign our variable like such:
var num = 5 // integer
var word = "hello" // string
var active = true // boolean
Now, this is an example of a weakly typed language. we are not declaring the type of data we are assigning to the variable instead of the code when parsed, identifies the variable as one of the types, and creates the data type accordingly.
Next is understanding the dynamic component of the language. Unless the script is in ‘strict mode’ (we’ll talk about this briefly later), we can dynamically use variables.
let's create two variables a and b. one will be an integer and the other a string
var a = 5 // integer
var v = "0" // string
Now let's pretend that we build a simple math function to add the two-variable what do you suppose will happen?
- Error because they are two different data types?
- It will be interpreted as a number and added resulting in a 5, or
- It will be interpreted as a string and added resulting in a ‘50'.
var a = 5 // integer
var v = "0" // stringa + b = "50" //interpret as string dynamically
This is how we declared variables. We will explore how to use these data types in the future.
Find reference
Primitive Types
Primitive types are often misconstrued as objects. In JS most things are objects, so I get the confusion. However, these are immutable data types. Since these are not objects all that means is that there are no methods or attributes assigned to the data type.
There is a caveat to that but I feel like we should wait to cover this later.
There are 6 primitive types in JS (and most languages).
- String — an array of characters, “a”, “apple”, “apples are red or green”
- Number — integers 1,2,3,5, floats 1.5, 2,68, 3.1456, etc
- Boolean — binary, true or false
- Null — no inherent value
- Undefined — a declared variable but hasn’t been assigned a value
- Symbol — a unique value that’s NOT equal to any other value
References main site
Objects (everything else)
Remember I said that JS is predominantly an object language. Well, aside from the primitive types everything else is considered an object. All that means is that these objects have both attributes and methods (aka functions).
There is a whole list of built-in objects in js already and you will often need to use these objects while coding. Objects store values which are either a value itself or another object like data or a function.
Here are some of the most commonly used built-in objects
Math() // with the methods allows to do mathmatics fucntions
Date() // with methods allows you to manipulate date and time
Promise() // with the methods allows you to create new promise objects
Let’s clear some confusion. I know said that primitives are not objects, i.e. they do not have attributes and methods, then why does this work?
var a = "abc"console.log(a.length) // expected value: 3
Primitives themselves are not objects instead when you declared a variable the parsing of the code recognizes that you attempted to declare a variable to a particular type and as a resolve uses a parent object to create the primitives within the contractor function in the parent object. the parent object also has methods which could be applied to the child primitive such as a.length
When you declare var a = "ABC" this is happening behind the scenes.
var a = new String('abc')
String() is a built-in object that takes the string declared variable and creates an instance of a string using the constructor function of the object itself. inside the object of the String() function, there are methods that can be applied to the child a such as a.length.
More on built-in objects here:
Wrap up: The Important Stuff
All you need to take away from this part of section one (Basics) is that in javascript you can declare variables several ways
var a = 0 // Number
a = 0 // Number
var a = new Number(0) // Number
You can dynamically type variables
var a = 0 // Number a == 0
var a = 5 // Number a == 5 now
var b = '0' // String b == '0'var c = a + b console.log(c) // String: '50'
Primitives types are 6 data types often used when declaring variables and they are not objects because they do not intrinsically have methods or attributes. However, once you declare the variable the parsing of the code recognizing the variable and passes it through a built-in object which allows the primitive type to somewhat inherent the attributes & methods of the parent object.
Everything else is considered an object which means it has either attributes, methods, and often both.
Quick examples of objects are built-in objects are
// actual objects (covering next)
var newObject = new Object() // Object // or object literals (covering next)
var newObject = {};// var newObject = new Function() // still an object// or functions
function newObject() {}// Math function
var newObject = Math.PI; // Still an object, pi (3.14...)// Date function
var newObject = new Date(); // Still an object, current date
That's it for now, a good thing to remember for the future lessons is that objects have methods and attributes that can be assigned, reassigned, and called. We will talk about that coming up!