Professional Documents
Culture Documents
JS Objects PDF
JS Objects PDF
1
JavaScript Objects
JavaScript variables are containers for data values.
This code assigns a simple value (Fiat) to a variable named
car:
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to
a variable named car:
objectName.propertyName
Or
objectName["propertyName“] Ex: car.type or car[“type”]
Note: refer always const keyword for objects
2
Object Methods
Objects can also have methods.
Methods are actions that can be performed on
objects.
Methods are stored in properties as function
definitions.
EX:
const person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
} };
3
Accessing Object Methods
You access an object method with the following syntax:
objectName.methodName()
Example:
name = person.fullName();
4
Java Script Built in Objects
Date Object
2. new Date(milliseconds);
3. new Date(dateString);
5
1. const d = new Date();
document.writeln(“date”+d);
6
JavaScript Get Date Methods
Method Description
getFullYear() Get the year as a four digit number (yyyy)
7
JavaScript Set Date Methods
Method Description
8
const d = new Date();
d.getFullYear();
9
Math Object
Unlike other objects, the Math object has no constructor.
The Math object is static.
All methods and properties can be used without creating a
Math object first.
10
JavaScript Math Methods
Method Description
abs(x) Returns the absolute value of x
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
cosh(x) Returns the hyperbolic cosine of x
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x, y, z, ..., Returns the number with the highest value
n)
min(x, y, z, ..., Returns the number with the lowest value
n)
pow(x, y) Returns the value of x to the power of y
11
random() Returns a random number between 0 and 1
<script>
document.getElementById("demo").innerHTML =
Math.sqrt(64);
</script>
12
String Object
A JavaScript string stores a series of characters
like “Good Luck".
A string can be any text inside double or single
quotes
Ex: Type 1 : String value
let carName1 = "Volvo XC60";
let carName2 = 'Volvo XC60';
13
When using the == operator, x and y are equal:
let x = "John"; // x is string value
let y = new String("John"); // y is string object
14
String Methods
Method Description
charAt() Returns the character at a specified index (position)
15
repeat() Returns a new string with a number of copies of a string
replace() Searches a string for a value, or a regular expression, and returns a string
where the values are replaced
search() Searches a string for a value, or regular expression, and returns the index
(position) of the match
slice() Extracts a part of a string and returns a new string
split() Splits a string into an array of substrings
startsWith() Checks whether a string begins with specified characters
substr() Extracts a number of characters from a string, from a start index (position)
substring() Extracts characters from a string, between two specified indices (positions)
toLocaleLowerCas Returns a string converted to lowercase letters, using the host's locale
e()
toLocaleUpperCas Returns a string converted to uppercase letters, using the host's locale
e()
toLowerCase() Returns a string converted to lowercase letters
16