KEMBAR78
Javascript Objects | PDF | Computer Science | Software Development
0% found this document useful (0 votes)
7 views17 pages

Javascript Objects

Uploaded by

yogitas804
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)
7 views17 pages

Javascript Objects

Uploaded by

yogitas804
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/ 17

JAVASCRIPT OBJECTS

Strings
A string in JavaScript is a sequence of characters enclosed in
either single or double quotes. For example, the following are
valid strings in JavaScript:
"Hello World"
'Hello World'
JavaScript provides a number of built-in methods for manipulating
strings. Some of the most commonly used string methods are:

length - This method returns the number of characters in a


string. For example, the following code will return 11:
var str = "Hello World";
console.log(str.length);

concat - This method is used to concatenate (combine) two or


more strings. For example, the following code will return "Hello
World":
var str1 = "Hello";
var str2 = " World";
console.log(str1.concat(str2));

indexOf - This method is used to find the index of a specific


character or substring in a string. For example, the following code
will return 6:
var str = "Hello World";
console.log(str.indexOf("W"));
slice - This method is used to extract a portion of a string. For
example, the following code will return "World":
var str = "Hello World";
console.log(str.slice(6));

replace - This method is used to replace a specific character or


substring in a string. For example, the following code will return
"Hello Universe":
var str = "Hello World";
console.log(str.replace("World", "Universe"));

toUpperCase and toLowerCase - These methods are used to


convert a string to uppercase or lowercase letters. For example,
the following code will return "HELLO WORLD" and "hello world"
respectively:
var str = "Hello World";
console.log(str.toUpperCase());
console.log(str.toLowerCase());
Arrays and Array Methods
One of the most important data structures in JavaScript is the
array, which is a collection of elements. In this blog post, we will
explore the basics of JavaScript arrays and the various array
methods that can be used to manipulate them.

An array in JavaScript is a collection of elements enclosed in


square brackets. Elements can be of any data type, including
numbers, strings, and other arrays. For example, the following is
a valid array in JavaScript:
var myArray = [1, "Hello", [2, 3]];
JavaScript provides a number of built-in methods for manipulating
arrays. Some of the most commonly used array methods are:

length - This method returns the number of elements in an array.


For example, the following code will return 3:
var myArray = [1, "Hello", [2, 3]];
console.log(myArray.length);

push - This method is used to add an element to the end of an


array. For example, the following code will add the element
"World" to the end of the array:
var myArray = [1, "Hello", [2, 3]];
myArray.push("World");
console.log(myArray); // [1, "Hello", [2, 3], "World"]

pop - This method is used to remove the last element of an array.


For example, the following code will remove the last element
("World") from the array:
var myArray = [1, "Hello", [2, 3], "World"];
myArray.pop();
console.log(myArray); // [1, "Hello", [2, 3]]

shift - This method is used to remove the first element of an


array. For example, the following code will remove the first
element (1) from the array:
var myArray = [1, "Hello", [2, 3]];
myArray.shift();
console.log(myArray); // ["Hello", [2, 3]]

unshift - This method is used to add an element to the beginning


of an array. For example, the following code will add the element
0 to the beginning of the array:
var myArray = [1, "Hello", [2, 3]];
myArray.unshift(0);
console.log(myArray); // [0, 1, "Hello", [2, 3]]

slice - This method is used to extract a portion of an array. For


example, the following code will extract the elements from index
1 to 2 (exclusive):
var myArray = [1, "Hello", [2, 3]];
console.log(myArray.slice(1, 2)); // ["Hello"]

splice - This method is used to add or remove elements from an


array. For example, the following code will remove the element at
index 1 and add the elements "Hello World" and [4, 5] at index 1:
var myArray = [1, "Hello", [2, 3]];
myArray.splice(1, 1, "Hello World", [4, 5]);
console.log(myArray);
Loops with Arrays
One of the most important data structures in JavaScript is the
array, which is a collection of elements. When working with
arrays, it is often necessary to iterate through each element in
the array, which is where loops come in. In this blog post, we will
explore how to use loops with arrays in JavaScript.
JavaScript provides several ways to iterate through an array,
including the for loop, forEach method, and for...of loop.

for loop - This is the most basic way to iterate through an array.
The for loop uses a counter variable that is incremented on each
iteration. For example, the following code will print out each
element in the array:
var myArray = [1, 2, 3, 4, 5];
for (var i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}

forEach method - This method is a more concise way to iterate


through an array. The forEach method takes a callback function
as its argument, which is called on each element in the array. For
example, the following code will print out each element in the
array:
var myArray = [1, 2, 3, 4, 5];
myArray.forEach(function(element) {
console.log(element);
});
for...of loop - This is a more recent addition to JavaScript, and it
is the most concise way to iterate through an array. It allows you
to iterate through the elements of an array without having to
access the index, and it works with any iterable object, not just
arrays. For example, the following code will print out each
element in the array:
var myArray = [1, 2, 3, 4, 5];
for (var element of myArray) {
console.log(element);
}
It is important to note that when you are iterating through an
array using a for loop and you plan to change the array during
iteration you should use a for loop with a separate counter
variable.
Map, Filter and Reduce
One of the most important data structures in JavaScript is the
array, which is a collection of elements. When working with
arrays, it is often necessary to manipulate the elements in various
ways, which is where the built-in methods map, filter, and reduce
come in. In this blog post, we will explore how to use these
methods to manipulate arrays in JavaScript.

Map - The map method is used to create a new array with the
result of a callback function called on each element in the original
array. The callback function takes one argument, the current
element, and returns the new value for that element. For
example, the following code will return an array of each element
multiplied by 2:
var myArray = [1, 2, 3, 4, 5];
var multipliedArray = myArray.map(function(element) {
return element * 2;
});
console.log(multipliedArray); // [2, 4, 6, 8, 10]

Filter - The filter method is used to filter an array based on a


certain condition. The filter method takes a callback function as
its argument, which is called on each element in the array. If the
function returns true, the element is included in the new filtered
array. For example, the following code will return an array of all
even numbers in the original array:
var myArray = [1, 2, 3, 4, 5];
var evenNumbers = myArray.filter(function(element) {
return element % 2 === 0;
});
console.log(evenNumbers); // [2, 4]

Reduce - The reduce method is used to reduce an array to a


single value. The reduce method takes a callback function as its
argument, which is called on each element in the array. The
callback function takes two arguments, the accumulator and the
current element, and returns the new value for the accumulator.
For example, the following code will return the sum of all
elements in the array:
var myArray = [1, 2, 3, 4, 5];
var sum = myArray.reduce(function(acc, cur) {
return acc + cur;
}, 0);
console.log(sum); // 15

It's important to note that these methods work on the original


array and doesn't change it, it returns a new array.
Combining these three methods can be powerful, for example,
you can filter an array, then map the filtered array, and finally
reduce the mapped array.
In conclusion, understanding and utilizing the map, filter, and
reduce methods in JavaScript can greatly improve your ability to
manipulate arrays and perform complex operations on them.
These methods are not only more concise but also more efficient
than traditional loops and make your code more readable.
Date
JavaScript's Date object allows you to work with dates and times
in your scripts. It can be used to get the current date and time, or
to manipulate and format dates and times in various ways.
Creating a new Date object is simple. You can either create a new
date object with the current date and time by calling new Date()
with no arguments, or you can create a date object with a specific
date and time by passing in a string or numerical values.
Here's an example of how you can use the Date object to get the
current date and time:

var currentDate = new Date();


console.log(currentDate);
Output: Thu Jan 14 2021 15:15:38 GMT+0530 (India Standard
Time)

You can also manipulate the date and time using the various
methods available on the Date object. For example, you can use
the setDate() method to set the day of the month, and the
setFullYear() method to set the year.

var date = new Date();

date.setDate(15);
date.setFullYear(2022);
console.log(date);
Output: Sat Jan 15 2022 15:15:38 GMT+0530 (India Standard
Time)
You can also format the date and time using the toLocaleString()
method, which allows you to specify the format and time zone.

var date = new Date();


console.log(date.toLocaleString());
console.log(date.toLocaleString('en-US', {timeZone: 'UTC'}));
Output: 1/14/2021, 3:45:38 PM 1/14/2021, 10:15:38 AM
In this way, you can use the Date object in JavaScript to work with
dates and times in your scripts, and to format and manipulate
dates and times in various ways.
Math
The JavaScript Math object is a built-in object that provides a
variety of mathematical functions and constants. It can be used to
perform mathematical operations such as trigonometry,
logarithms, and random number generation.
One of the most commonly used functions in the Math object is
the Math.random() function, which generates a random number
between 0 (inclusive) and 1 (exclusive). This function can be used
to generate random numbers for various purposes such as
games, simulations, and other applications.

console.log(Math.random());
// Output: a random number between 0 and 1 (e.g. 0.3456)
You can also use the Math.floor() function to round a number
down to the nearest integer.

console.log(Math.floor(3.8));
// Output: 3
The Math.ceil() function rounds a number up to the nearest
integer.

console.log(Math.ceil(3.2));
// Output: 4
Another useful function of Math object is Math.max() and
Math.min() which are used to find the maximum and minimum
values in a set of numbers respectively.

console.log(Math.max(3,5,7,9));
// Output: 9
console.log(Math.min(3,5,7,9));
// Output: 3
Math object also provides the most commonly used mathematical
constants such as Math.PI for the value of pi, Math.E for the value
of the mathematical constant e.

console.log(Math.PI);
// Output: 3.141592653589793
console.log(Math.E);
// Output: 2.718281828459045
In this way, you can use the Math object in JavaScript to perform
mathematical operations, generate random numbers, and access
mathematical constants. It is a powerful tool that can be used to
add more functionality to your JavaScript programs and make
them more interactive and dynamic.
Number
The JavaScript Number object is a built-in object that provides a
number of properties and methods for working with numeric
values. The Number object can be used to convert a value to a
number, perform mathematical operations, and format numbers
for display.
One of the most commonly used methods of the Number object is
the Number() function, which can be used to convert a value to a
number. This function can be used to convert strings, booleans,
and other types of values to numbers.

console.log(Number("3.14"));
// Output: 3.14
console.log(Number(true));
// Output: 1
You can also use the parseInt() and parseFloat() methods to
convert strings to integers and floating-point numbers,
respectively.

console.log(parseInt("3"));
// Output: 3
console.log(parseFloat("3.14"));
// Output: 3.14
The Number object also provides several properties that can be
used to access the maximum and minimum values that can be
represented by a number in JavaScript. These properties are
Number.MAX_VALUE and Number.MIN_VALUE.

console.log(Number.MAX_VALUE);
// Output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE);
// Output: 5e-324
Another useful property of the Number object is
Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY which
represents the positive and negative infinity respectively.

console.log(1 / 0);
// Output: Infinity
console.log(-1 / 0);
// Output: -Infinity
The Number object also provides several methods for formatting
numbers for display, such as the toFixed() method, which can be
used to format a number with a specific number of decimal
places.

console.log((3.1415926535897932384626433832795).toFixed(2))
;
// Output: "3.14"

In this way, you can use the Number object in JavaScript to


convert values to numbers, perform mathematical operations,
and format numbers for display. It is a powerful tool that can be
used to add more functionality to your JavaScript programs and
make them more interactive and dynamic.
Boolean
The JavaScript Boolean object is a built-in object that provides a
way to work with Boolean (true/false) values in your scripts. The
Boolean object can be used to create Boolean values and perform
logical operations, such as and, or, and not.
A boolean value can be created by using the Boolean() function or
by assigning a value of true or false to a variable.

var isTrue = Boolean(1);


console.log(isTrue); // Output: true
var isFalse = Boolean(0);
console.log(isFalse); // Output: false
You can also use the logical && operator to check if both
expressions are true and the || operator to check if at least one of
the expressions is true.

var x = true;
var y = false;
console.log(x && y); // Output: false
console.log(x || y); // Output: true
You can also use the ! operator to invert a boolean value.

console.log(!x); // Output: false


console.log(!y); // Output: true
The Boolean object also provides a valueOf() method that returns
the primitive value of the Boolean object
var b = new Boolean(true);
console.log(b.valueOf()); // Output: true
In this way, you can use the Boolean object in JavaScript to work
with Boolean values, perform logical operations, and invert
Boolean values. It is a fundamental part of programming and
helps to control the flow of the program.

You might also like