Reference Types
- In the reference types category, you have objects, arrays, and functions.
Object
- An object in JavaScript and any other programming languages is like an object in real life. eg car, mobile, person
- Think of a person. A person has name, age, address and so on. These are the properties of a person.
- So when you're dealing with multiple related variables, you can put these variables inside of an object.
let name = "ganesh";
let age = "20";
- Here you have two variables name and age, they're highly related, they are part of the representation of a person, so instead of declaring two variables, you can declare the person object and reference the person object, it makes our code cleaner.
Syntax
- You create a JavaScript object with an object literal
- You add one or more
key:value
pairs inside the object. The keys are the properties of the object.
let person = { name: "ganesh", age: 20 }
Accessing Properties
- There are two ways to work with these properties: dot notation and bracket notation
Dot notation
- Shorter
Read property console.log(person.name);
Write property person.name = "kumar"
Bracket notation
- Useful when the property is available at runtime
Read property console.log(person["name"]);
Write property person["name"] = "kumar"
Arrays
- Array, is a data structure to represent a list of items.
- For example, the list of products in a shopping cart, or the list of countries
Creating an Array
- Square brackets is array literal, and they indicate an empty array.
let countries = []; //Array Literal
Initialize an Array
let countries = ["India", "US"];
- Now we can initialize this array and add a couple of items
Items
- Each item has an index, and that determines that position of the item in that array.
- Length of arrays as well as the type of objects you have in an array are dynamic,
- You can add another item to this array, so the array will expand.
- Unlike other programming languages, where every item or every object in the array, should have the same type, in JavaScript you can store different types in an array.
- Technically an array is an object, it has a bunch of key value pairs, or properties that you can access using the dot notation
- Length property returns a number of items or elements in an array
Functions
- A JavaScript function is a set of statements that either performs a task or calculates and returns a value.
Syntax
function name() {
// code to be executed
}
function name(parameter1, parameter2, parameter3) {
// code to be executed
}
Declaring Function
function greet() {
console.log("Hello World");
}
Invoking/Calling Function
greet();
Parameters
- Functions can have inputs/parameters, and these inputs can change how the function behaves.
- Parameter is like a variable that is accessible inside of the function and is not accessible outside of the function
- Argument is the actual value you supply for the parameter.
- You can reuse function but with a different input.
- Function can have multiple parameters. Separate parameters using a comma
Type of Functions
- Performing a task
- Calculate something
A real world application is essentially a collection of hundreds or thousands of functions working together to provide the functionality of that application.