Java Script
Java Script
In JavaScript, data types represent the kind of value a variable can hold.
JavaScript has both primitive and non-primitive (also called reference) data types.
These are the most basic types of data in JavaScript. They are immutable, meaning once
created, their values cannot be changed.
Undefined: Represents a variable that has been declared but not assigned a value.
o Example: If a variable is declared but not given a value, it's automatically
undefined.
o Example:
let user;
console.log(user); // undefined
These types are more complex and are stored as references to their memory location.
let person = {
name: "John",
age: 30,
isEmployed: true
};
Array: A special type of object used to store ordered collections of values (elements).
o Example:
Function: A block of code designed to perform a specific task. Functions are also
objects in JavaScript.
o Example:
function greet() {
console.log("Hello!");
}
Variables are containers for storing data values. In JavaScript, there are three ways to declare
variables:
// Number
let age = 25;
// Boolean
let isAdult = true;
// Object
let person = {
name: "John",
age: 30
};
// Array
let colors = ["red", "blue", "green"];
// Function
function greet() {
console.log(greeting);
}
In JavaScript, objects are a fundamental data type used to store collections of data in key-
value pairs. Each key (also called a property) is associated with a value, which can be a
primitive value (like a string, number, etc.), another object, or even a function. This key-value
structure makes objects ideal for storing complex data and organizing it in a more readable
and meaningful way.
1. Properties: These are the keys (or fields) of the object. Each property holds a value, which
can be any data type (string, number, array, etc.).
2. Methods: These are functions that are defined within an object. Methods can perform
operations on the object's properties or can carry out some other logic.
Define an object in JavaScript using either object literal notation or the new Object()
syntax.
This is the most common and simple way to define an object. It uses curly braces {} to
enclose the key-value pairs.
let person = {
name: "John", // Property
age: 30, // Property
greet: function() { // Method (function as a property)
console.log("Hello, my name is " + this.name);
}
};
Both the object literal and new Object() methods create the same object, but the object
literal is preferred for its simplicity and readability.
dot notation
bracket notation.
1. Dot Notation:
Use bracket notation, especially if the property names contain spaces or are dynamically
generated.
Bracket notation is useful when you want to access a property dynamically (e.g., using a
variable).
The this keyword inside a method refers to the object the method is called on. It allows the
method to access other properties of the object.
let person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello, my name is " + this.name);
}
};
person.greet(); // Outputs: Hello, my name is John
console.log(person[propertyName]); // Outputs: 30
let car = {
make: "Toyota",
model: "Camry",
year: 2021,
drive: function() {
console.log(this.make + " " + this.model + " is driving.");
},
stop: function() {
console.log(this.make + " " + this.model + " has stopped.");
},
getCarInfo: function() {
return `${this.year} ${this.make} ${this.model}`;
}
};