0% found this document useful (0 votes)
5 views7 pages

Java Script

JavaScript objects are key-value pairs used to store data and can be created using object literal or constructor function syntax. Functions in JavaScript allow for code reusability and can be declared in various ways, including traditional function declarations, function expressions, and arrow functions. Additionally, JavaScript provides variable declarations using var, let, and const, each with distinct scoping rules, and supports modular programming through ES6 modules for better code organization and maintainability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Java Script

JavaScript objects are key-value pairs used to store data and can be created using object literal or constructor function syntax. Functions in JavaScript allow for code reusability and can be declared in various ways, including traditional function declarations, function expressions, and arrow functions. Additionally, JavaScript provides variable declarations using var, let, and const, each with distinct scoping rules, and supports modular programming through ES6 modules for better code organization and maintainability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Objects in JavaScript

In JavaScript, objects are a fundamental data structure used to store collections of data and
more complex entities. They consist of a set of key-value pairs where each key (or property) is
a string, and each value can be of any data type: number, string, boolean, function, or even
another object. Objects are the foundation for structuring and organizing data in JavaScript
programs.

Creating an Object

You can create an object in JavaScript using either the object literal syntax or the constructor
function syntax.

1. Object Literal Syntax

This is the most commonly used method to create objects in JavaScript. Here's an example:

let car = {
brand: "Toyota",
model: "Corolla",
year: 2023,
start: function() {
console.log("The car has started!");
}
};

● In the above example:


○ brand, model, and year are properties of the car object.
○ start is a method of the car object, which is a function stored as a property of
the object.

2. Constructor Function Syntax

You can also create an object using a constructor function:

function Car(brand, model, year) {


this.brand = brand;
this.model = model;
this.year = year;
this.start = function() {
console.log("The car has started!");
};
}

let myCar = new Car("Toyota", "Corolla", 2023);

● Here, the Car function acts as a constructor to create new Car objects. The new
keyword is used to create an instance of the object, and the this keyword is used to
refer to the new object being created.

Accessing Object Properties

To access properties of an object, you use either dot notation or bracket notation.

1. Dot Notation
console.log(car.brand); // Output: Toyota

2. Bracket Notation
console.log(car["model"]); // Output: Corolla

Dot notation is more common, but bracket notation is useful when the property name is dynamic
or if it contains spaces or special characters.

Accessing Object Methods

To call a method of an object, you can use the dot notation as well:

car.start(); // Output: The car has started!

Functions in JavaScript
Functions are reusable blocks of code that perform a specific task. Functions enable
modularity, reusability, and better organization in your code. You can declare a function in
several ways in JavaScript, each with slightly different syntax and behaviors.

1. Function Declaration (Traditional Function)


The function declaration syntax is the most familiar way to define a function in JavaScript.

function greet() {
console.log("Hello, World!");
}

greet(); // Output: Hello, World!

● This method allows you to declare a function and call it anywhere in your code, even
before the function is defined. This is due to hoisting, where JavaScript moves function
declarations to the top of their scope.

2. Function Expression

A function expression involves assigning a function to a variable. This can be either an


anonymous function or a named function.

let greet = function() {


console.log("Hello, World!");
};

greet(); // Output: Hello, World!

● The function is anonymous because it doesn't have a name. Function expressions are
not hoisted, meaning you cannot call the function before it's defined.

3. Arrow Functions (ES6)

Arrow functions, introduced in ECMAScript 6 (ES6), provide a more concise syntax for writing
functions. They also have a unique behavior with respect to the this keyword, which will be
covered later.

let greet = () => {


console.log("Hello, World!");
};

greet(); // Output: Hello, World!


● Arrow functions are often used when you need a short function without the overhead of
function declarations. Arrow functions can also have implicit returns when the body
consists of a single expression.

let add = (a, b) => a + b;


console.log(add(2, 3)); // Output: 5

The this Keyword

In JavaScript, this refers to the context in which a function is executed. It is commonly used
inside methods to refer to the object that owns the method.

Example with Object Method


let person = {
name: "Alice",
greet: function() {
console.log(this.name); // 'this' refers to the person object
}
};

person.greet(); // Output: Alice

In this case, this refers to the person object, and this.name accesses the name property of
the object.

Arrow Functions and this

Arrow functions do not have their own this. Instead, they inherit this from their enclosing
scope. This behavior can be useful in some cases where you want to maintain the this context
from the outer function.

let person = {
name: "Alice",
greet: () => {
console.log(this.name); // 'this' does not refer to 'person'
}
};

person.greet(); // Output: undefined


In this example, this inside the arrow function does not refer to the person object, because
arrow functions don’t have their own this. Instead, they inherit this from their surrounding
context.

Difference Between var, let, and const


JavaScript provides three ways to declare variables: var, let, and const. Each has different
scoping and reassignment rules.

1. var

The var keyword is the oldest way to declare variables in JavaScript. Variables declared with
var are function-scoped, meaning they are accessible throughout the function they are
declared in. If declared outside any function, they become globally scoped.

function example() {
var a = 10;
if (true) {
var a = 40; // 'a' is redeclared, and its value is
overwritten
}
console.log(a); // Output: 40
}
example();

● var is prone to errors because it can be redeclared in the same scope, and its behavior
can be confusing due to hoisting.

2. let

The let keyword was introduced in ES6 to provide block-level scoping. It is more predictable
than var because it is limited to the block (enclosed by curly braces) where it is defined.

function example() {
let b = 20;
if (true) {
let b = 50; // This 'b' is in a different block scope
console.log(b); // Output: 50
}
console.log(b); // Output: 20
}
example();

● let cannot be redeclared in the same scope, making it safer and more predictable than
var.

3. const

The const keyword is also block-scoped like let, but it is used for constants—values that
cannot be reassigned.

function example() {
const c = 30;
// c = 40; // Error: Assignment to constant variable
console.log(c); // Output: 30
}
example();

● While the variable itself cannot be reassigned, if the constant is an object or an array, its
contents can be modified.

const person = { name: "John" };


person.name = "Alice"; // Allowed, because we're modifying the
object, not reassigning it.
console.log(person.name); // Output: Alice

JavaScript Modules
JavaScript modules allow you to split your code into separate files, making it more maintainable
and easier to understand. This modular approach is useful for large applications where you
need to organize different pieces of code.
Importance of Modules

1. Reusability: You can create reusable functions or variables that can be imported into
multiple files.
2. Maintainability: Splitting code into smaller modules makes it easier to manage and
debug.
3. Separation of Concerns: Different parts of the application can be encapsulated in
different modules, promoting cleaner architecture.

Using ES6 Modules

ECMAScript 6 (ES6) introduced modules to JavaScript. Here's how to use them.

Exporting from a Module

You can export functions, objects, or variables from one file using the export keyword:

// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

Importing a Module

To use the functions from math.js in another file, you use the import keyword:

// main.js
import { add, subtract } from './math.js';

console.log(add(2, 3)); //

You might also like