Javascript Fundamentals: A breakdown
VARIABLES
- What is a variable?
Quite simply a variable is a placeholder for an expression that can change or ‘vary’ — hence why it is called a variable.
Think of a variable as a container that holds information.
- Why do we need variables?
Using variables gives programmers a way to label data with a descriptive name so that the program can be understood more clearly.
Variables are often named in conjunction to the program they are being used for.
For example if you were writing a program that will add family members to a list you may have the following variables:
lastName
firstName
- How do you declare a variable? what does it mean to declare a variable?
Variables can be declared in JavaScript using the following words followed by the (=) operator.
Note: The (=) operator allows you to assign a value to the variable.
let lastName = "Iron"const firstName = "Flat"*var nameGuest = "Student"
- How do you update a variable?
Let’s say we no longer want lastName to be assigned to “Iron”.
We can just rewrite the let statement and assign a new lastName.
let lastName = "Iron"console.log lastName() // "Iron"let lastName = "Graduate"console.log lastName // "Graduate"
FUNCTIONS
- What is a function?
A function quite simply a set of instructions. Typically written as an order of steps (statements).
You need to decide on a name for your function and determine with variable your function will depend upon.
If we use the example from variable collecting family members names, we would want to name our function something related to the variables.
So for the example function we would name it ‘familyMembers’.
This function would take in a first and last name variable:
function familyMembers(lastName, firstName) {return `lastName, + firstName`};
- Invoking functions
In order to get this function (set of instructions) to run, we ‘call’ the function by it’s name. Like so:
function familyMembers();
// "Iron Graduate"
If the variables are defined properly, and the funciton is called correctly, it should return the concatnated string “Iron Graduate”.
DATA TYPES
Data types describe the different data categories that can be used to store data. There are 6 primitive data types: numbers, strings, boolean, symbol, bigInt, and undefined.
Example of an number data type:
let y = 53
// number data type
Example of an Undefined data type:
let unassignedVariable;
// => undefined
Example of a String data type:
let x = "Dakota the dog"
// assigns x with a string
Data Structures
Data structures are a method to organize, categorize, and store the data. There are 3 main type of data structures to consider: arrays, linked nodes, and hash tables. Arrays are similar to a list. A linked node is a pointer to other objects. Lastly, a hash table is a structure where keys are paired with values.
Example of an Array:
let dogs = ['labador retriever', 'german shepard', 'golden retriever']
// Array of dog types
Interacting and Accessing Data
When interacting and accessing data, there are 2 approaches: dot notation and bracket notation. The dot notation is the preferred notation, while the bracket notation is typically used for when variables are used instead of keys.
let address= {street: "Main", city: "Houston", state: "Texas"}
let addressStreet = address.street
let city = address["city"]