top of page

Javascript Notes

mobiprep (6).png

JavaScript

mobiprep (6).png

Data Types

mobiprep (6).png

Variables

mobiprep (6).png

Operators

mobiprep (6).png

Functions

mobiprep (6).png

Conditional Statements and Loops

mobiprep (6).png

Arrays

mobiprep (6).png

Objects

mobiprep (6).png

DOM Elements

mobiprep (6).png

Cookies

mobiprep (6).png

Pop Up Box

mobiprep (6).png

Exception Handling

Heading

Q

1

Explain JavaScript Objects.

LRM_EXPORT_207556595493866_20190724_1939

Ans

A javaScript object is an entity having state and behavior (properties and method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.

JavaScript is an object-based language. Everything is an object in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Q

2

What are the three different ways to create an object in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

Ans

There are 3 ways to create objects.

By object literal
By creating instance of Object directly (using new keyword)
By using an object constructor (using new keyword)

1) JavaScript Object by object literal

The syntax of creating object using object literal is given below:

object={property1:value1,property2:value2.....propertyN:valueN}

As you can see, property and value is separated by : (colon).

Let’s see the simple example of creating object in JavaScript.

<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>

Output
102 Shyam Kumar 40000

2) By creating instance of Object

The syntax of creating object directly is given below:

var objectname=new Object();

Here, new keyword is used to create object.

Let’s see the example of creating object directly.

<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>

Output
101 Ravi 50000

3) By using an Object constructor

Here, you need to create function with arguments. Each argument value can be assigned in the current object by using this keyword.

The this keyword refers to the current object.

The example of creating object by object constructor is given below.

<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"Vimal Jaiswal",30000);

document.write(e.id+" "+e.name+" "+e.salary);
</script>

Output
103 Vimal Jaiswal 30000

LRM_EXPORT_207556595493866_20190724_1939

Q

3

Define Object properties in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

1.Object.assign() This method is used to copy enumerable and own properties from a source object to a target object
2 Object.create() This method is used to create a new object with the specified prototype object and properties.
3 Object.defineProperty() This method is used to describe some behavioral attributes of the property.
4 Object.defineProperties() This method is used to create or configure multiple object properties.
5 Object.entries() This method returns an array with arrays of the key, value pairs.
6 Object.freeze() This method prevents existing properties from being removed.
7 Object.getOwnPropertyDescriptor() This method returns a property descriptor for the specified property of the specified object.
8 Object.getOwnPropertyDescriptors() This method returns all own property descriptors of a given object.
9 Object.getOwnPropertyNames() This method returns an array of all properties (enumerable or not) found.
10 Object.getOwnPropertySymbols() This method returns an array of all own symbol key properties.
11 Object.getPrototypeOf() This method returns the prototype of the specified object.
12 Object.is() This method determines whether two values are the same value.
13 Object.isExtensible() This method determines if an object is extensible
14 Object.isFrozen() This method determines if an object was frozen.
15 Object.isSealed() This method determines if an object is sealed.
16 Object.keys() This method returns an array of a given object's own property names.
17 Object.preventExtensions() This method is used to prevent any extensions of an object.
18 Object.seal() This method prevents new properties from being added and marks all existing properties as non-configurable.
19 Object.setPrototypeOf() This method sets the prototype of a specified object to another object.
20 Object.values() This method returns an array of values.

LRM_EXPORT_207556595493866_20190724_1939

Q

4

Explain the use of 'this' keyword in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

The this keyword is one of the most widely used and yet confusing keyword in JavaScript. Here, you will learn everything about this keyword.
this points to a particular object. Now, which is that object is depends on how a function which includes 'this' keyword is being called.

LRM_EXPORT_207556595493866_20190724_1939

Q

5

Define Screen Objects in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

The screen object contains information about the visitor's screen.

Note: There is no public standard that applies to the screen object, but all major browsers support it.

LRM_EXPORT_207556595493866_20190724_1939
bottom of page