0% found this document useful (0 votes)
3 views5 pages

Java Script

javascript

Uploaded by

vanitha.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Script

javascript

Uploaded by

vanitha.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Explain the different data types in java script and how variables are used

How java script objects work

 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.

1. Primitive Data Types:

These are the most basic types of data in JavaScript. They are immutable, meaning once
created, their values cannot be changed.

 String: Represents a sequence of characters.


o Example: "Hello, World!"
o Variables that hold strings can be declared with single quotes ('), double
quotes ("), or backticks (`) for template literals.
o Example: let name = "Alice";
 Number: Represents both integer and floating-point numbers.
o Example: 42, 3.14, -10
o Numbers in JavaScript are always 64-bit floating-point, which means there's
no distinction between integers and floating-point numbers.
o Example:

let age = 25;

let price = 99.99;

 Boolean: Represents a logical value that can either be true or false.


o Example: true, false
o Example:

let isActive = true;

 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

 Null: Represents the intentional absence of any value or object.


o Example: null is a special value that indicates "no value".
o Example:

let car = null;

2. Non-Primitive Data Types (Reference Types):

These types are more complex and are stored as references to their memory location.

 Object: Represents a collection of key-value pairs (properties and methods). Objects


can contain many different types of values.
o Example:

let person = {
name: "John",
age: 30,
isEmployed: true
};

 Array: A special type of object used to store ordered collections of values (elements).
o Example:

let numbers = [1, 2, 3, 4, 5];

 Function: A block of code designed to perform a specific task. Functions are also
objects in JavaScript.
o Example:

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

How Variables are Used in JavaScript:

Variables are containers for storing data values. In JavaScript, there are three ways to declare
variables:

1. var (older, less commonly used today):


o var is function-scoped (if declared inside a function) or globally scoped if
declared outside.
o Example:

var name = "Alice";

2. let (recommended for block-scoping):


o let is block-scoped, meaning it’s confined to the block in which it’s declared
(like inside loops or conditionals).
o Example:

let age = 30;


if (true) {
let age = 25; // This `age` is different from the outer one.
}
console.log(age); // Outputs: 30

3. const (for constant values):


o const is also block-scoped like let, but the value of a variable declared with
const cannot be reassigned after it is assigned a value.
o Example:

const birthYear = 1990;


// birthYear = 1991; // This would throw an error!

Example of Variable Usage:


// String
let greeting = "Hello";

// 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);
}

greet(); // Outputs: Hello

How JavaScript Objects Work

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.

Key Concepts of JavaScript Objects:

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.

Defining Objects in JavaScript

Define an object in JavaScript using either object literal notation or the new Object()
syntax.

1. Using Object Literal Notation:

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);
}
};

In the above example:

 name and age are properties.


 greet is a method that prints a message when called.

2. Using new Object() Syntax:

This is less common but can still be used to create objects.

let person = new Object();


person.name = "John";
person.age = 30;
person.greet = function() {
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.

Accessing Properties and Methods

Access object properties and methods in two ways:

dot notation

bracket notation.

1. Dot Notation:

This is the most common way to access an object's properties or methods.

console.log(person.name); // Outputs: John


console.log(person.age); // Outputs: 30
person.greet(); // Outputs: Hello, my name is John
2. Bracket Notation:

Use bracket notation, especially if the property names contain spaces or are dynamically
generated.

console.log(person["name"]); // Outputs: John


person["greet"](); // Outputs: Hello, my name is John

Bracket notation is useful when you want to access a property dynamically (e.g., using a
variable).

let propertyName = "age";


this Keyword in Objects

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

Example: Complete JavaScript Object

Here’s an example of an object that defines both properties and methods:

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}`;
}
};

console.log(car.getCarInfo()); // Outputs: 2021 Toyota Camry


car.drive(); // Outputs: Toyota Camry is driving.
car.stop(); // Outputs: Toyota Camry has stopped.

You might also like