Unit1 Basics of JavaScript
Unit1 Basics of JavaScript
Unit 1
Basics of JavaScript Programming
1
Client-Side Scripting (Semester-V)
Introduction:
The scripts can be written in two forms, at the server end (back end) or at
the client end (server end). The main difference between server-side
scripting and client-side scripting is that the server-side scripting involves
server for its processing. On the other hand, client-side scripting requires
browsers to run the scripts on the client machine but does not interact with
the server while processing the client-side scripts.
A script is generally a series of program or instruction, which has to be
executed on other program or application. As we know that the web works in
a client-server environment. The client-side script executes the code to the
client side which is visible to the users while a server-side script is executed
in the server end which users cannot see.
Source: https://techdifferences.com/difference-between-server-side-
scripting-and-client-side-scripting.html
The server-side involves four parts: server, database, API’s and back-end
web software developed by the server-side scripting language. When a
2
Client-Side Scripting (Semester-V)
3
Client-Side Scripting (Semester-V)
BASIS FOR
COMPARIS SERVER-SIDE SCRIPTING CLIENT-SIDE SCRIPTING
ON
Basic Works in the back end which Works at the front end and
could not be visible at the script are visible among the
client end. users.
4
Client-Side Scripting (Semester-V)
5
Client-Side Scripting (Semester-V)
Advantages of JavaScript:
Speed: Client-side JavaScript is very fast because it can be run
immediately within the client-side browser. Unless outside resources
are required, JavaScript is unhindered by network calls to a backend
server.
Simplicity: JavaScript is relatively simple to learn and implement.
Popularity: JavaScript is used everywhere on the web.
Interoperability: JavaScript plays nicely with other languages and
can be used in a huge variety of applications.
Server Load: Being client-side reduces the demand on the website
server.
Light-weight and interpreted: JavaScript is a lightweight scripting
language because it is made for data handling at the browser only.
Since it is not a general-purpose language so it has a limited set of
libraries. Also, as it is only meant for client-side execution and that too
for web applications, hence the lightweight nature of JavaScript is a
great feature. JavaScript is an interpreted language which means the
script written inside JavaScript is processed line by line. These Scripts
are interpreted by JavaScript interpreter which is a built-in component
of the Web browser. But these days many JavaScript engines in
browsers like the V8 engine in chrome uses just in time compilation for
JavaScript code.
Gives the ability to create rich interfaces.
Client-side Validations: This is a feature which is available in
JavaScript since forever and is still widely used because every website
has a form in which users enter values, and to make sure that users
enter the correct value, we must put proper validations in place, both
on the client-side and on the server-side. JavaScript is used for
implementing client-side validations.
Disadvantages of JavaScript:
Security: As the code executes the user’s computer, in some cases it
can be exploited for malicious purpose.
Javascript done not read and write the files.
Javascript can not be used for networking applications.
Javascript does not have multi-threading and multi-processing
capabilities.
Javascript does not support overloading and overriding.
6
Client-Side Scripting (Semester-V)
Object Name:
Each object is uniquely identified by a name or ID.
With JavaScript, you can define and create your own objects.
There are different ways to create new objects:
7
Client-Side Scripting (Semester-V)
Code:
<html>
<body>
<script>
emp={id:"VP-179",name:"Aaa Bbb",salary:50000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
</body>
</html>
Output:
VP-179 Aaa Bbb 50000
Code:
<html>
<body>
<script>
var emp=new Object();
emp.id="VP-179";
emp.name="Aaa ";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
</body>
</html>
Output:
VP-179 Aaa 50000
8
Client-Side Scripting (Semester-V)
Code:
<html>
<body>
<script>
function emp(id,name,salary)
{
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp("VP-179","Aaa ",50000);
document.write(e.id+" "+e.name+" "+e.salary);
</script>
</body>
</html>
Output:
VP-179 Aaa 50000
Types of Objects:
• Native Objects/ Built-in Objects: are those objects supplied by
JavaScript.
Examples of these are Math, Date, String, Number, Array, Image, etc.
1) Math:
Math Properties
9
Client-Side Scripting (Semester-V)
Math Description
Property
SQRT2 Returns square root of 2.
PI Returns Π value.
E Returns Euler's Constant.
LN2 Returns natural logarithm of 2.
LN10 Returns natural logarithm of 10.
Code:
<html>
<head>
<title>JavaScript Math Object Properties</title>
</head>
<body>
<script type="text/javascript">
var value1 = Math.E;
document.write("E Value is :" + value1 + "<br>");
var value3 = Math.LN10;
document.write("LN10 Value is :" + value3 + "<br>");
var value4 = Math.PI;
document.write("PI Value is :" + value4 + "<br>");
</script>
</body>
</html>
Output:
E Value is :2.718281828459045
LN10 Value is :2.302585092994046
PI Value is :3.141592653589793
Math Methods
Math Description
Methods
abs() Returns the absolute value of a number.
acos() Returns the arccosine (in radians) of a number.
ceil() Returns the smallest integer greater than or equal to a
cos() Returns cosine of a number.
floor() Returns the largest integer less than or equal to a
10
Client-Side Scripting (Semester-V)
11
Client-Side Scripting (Semester-V)
Code:
<html>
<body>
<h2>Date Methods</h2>
<script type="text/javascript">
var d = new Date();
document.write("<b>Locale String:</b> " + d.toLocaleString()+"<br>");
document.write("<b>Hours:</b> " + d.getHours()+"<br>");
document.write("<b>Day:</b> " + d.getDay()+"<br>");
document.write("<b>Month:</b> " + d.getMonth()+"<br>");
document.write("<b>FullYear:</b> " + d.getFullYear()+"<br>");
document.write("<b>Minutes:</b> " + d.getMinutes()+"<br>");
</script>
</body>
12
Client-Side Scripting (Semester-V)
</html>
Example:
var s = new String(string);
String Properties:
String properties Description
length It returns the length of the string.
constructor It returns the reference to the String
String Methods:
String Description
methods
charAt() It returns the character at the specified index.
charCodeAt( It returns the ASCII code of the character at the specified
concat() It combines the text of two strings and returns a new string.
indexOf() It returns the index within the calling String object.
match() It is used to match a regular expression against a string.
replace() It is used to replace the matched substring with a new
search() It executes the search for a match between a regular
slice() It extracts a session of a string and returns a new string.
13
Client-Side Scripting (Semester-V)
• Host Objects: are objects that are supplied to JavaScript by the browser
environment. Examples of these are window, document, forms, etc.
Window:
The window object represents a window in browser.
An object of window is created automatically by the browser.
Window is the object of browser; it is not the object of javascript.
Window Methods:
Window Description
methods
14
Client-Side Scripting (Semester-V)
Output:
Property:
Properties are the values associated with a JavaScript object.
A JavaScript object is a collection of unordered properties.
Properties can usually be changed, added, and deleted, but some
are read only.
15
Client-Side Scripting (Semester-V)
Methods:
JavaScript methods are actions that can be performed on objects.
A JavaScript function is a block of code designed to perform a particular
task.
A JavaScript function is defined with the function keyword, followed by a
name, followed by parentheses ().
The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets:
{}
16
Client-Side Scripting (Semester-V)
Syntax:
function name(parameter1, parameter2, parameter3)
{
// code to be executed
}
Main Event:
An event is an action performed by user or web browser.
In order to make a web pages more interactive, the script needs to be
accessed the contents of the document and know when the user is
interacting with it.
Events may occur due to: 1) a document loading
2) user clicking on mouse button
3) browser screen changing size
Here are some examples of HTML events:
• An HTML web page has finished loading
• An HTML input field was changed
• An HTML button was clicked
Event handlers can be used to handle, and verify, user input, user actions,
and browser actions:
Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseov The user moves the mouse over an HTML element
onmouseou The user moves the mouse away from an HTML
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page
18
Client-Side Scripting (Semester-V)
Code:
<html>
<head>
<script type="text/javascript">
function msg()
{
alert("Hello IF5I students");
}
</script>
</head>
<body>
<center>
<p><h1>Welcome to Client-side scripting</h1></p>
<form>
<input type="button" value="click" onclick ="msg()"/> // onclick event
</form>
</body>
</html>
Output:
19
Client-Side Scripting (Semester-V)
The getElementById() method returns the elements that has given ID which
is passed to the function.
This function is widely used in web designing to change the value of any
particular element or get a particular element.
Syntax: document. getElementById(element_id) ;
Parameter: This function accepts single parameter element_id which is used
to hold the ID of element.
Return Value: It returns the object of given ID. If no element exists with given
ID then it returns null.
Code:
<html>
<body>
<p id="demo">Click the button to change the color of this paragraph.</p>
<button onclick="myFunction()">change color</button>
<script>
function myFunction()
{
var x = document.getElementById("demo");
x.style.color = "red";
}
</script>
</body>
</html>
Output:
20
Client-Side Scripting (Semester-V)
We can say that variable is a container that can be used to store value and
you need to declare a variable before using it.
In JavaScript, the var keyword is used to declare a variable. Also, starting
from ES6 we can also declare variables using the let keyword.
JavaScript Syntax for Declaring a Variable
Following is the syntax for declaring a variable and assigning values to it.
var varible-name;
or
let varible-name;
We can also define a variable without using the semicolon too. Also, when we
have to define multiple variables, we can do it like this:
var x,y,z;
or
var x,y,z
21
Client-Side Scripting (Semester-V)
between the opening and closing curly braces { }, when declared and
defined inside a code block or a function body.
Starting from ES6 it is recommended to use the let keyword while declaring
local variables.
A JavaScript local variable is declared inside block or function.
It is accessible within the function or block only.
For example:
<script>
function abc()
{
var x=10; //x is a local variable
}
</script>
<html>
<body>
<script>
var data=200; //gloabal variable
function a()
{
document.write(data);
}
function b()
{
22
Client-Side Scripting (Semester-V)
document.write(data);
}
a(); //calling JavaScript function
b();
</script>
</body>
</html>
Output:
200 200
23
Client-Side Scripting (Semester-V)
24
Client-Side Scripting (Semester-V)
Output:
uncaught ReferenceError: x is not defined
In the above example, the variable is accessible only inside the block. See
the below example, to see that:
{
let x = 2;
alert(x) // accessible
}
Output:
2
25
Client-Side Scripting (Semester-V)
}
alert(amount) // not accessible
Output:
Uncaught ReferenceError: amount is not defined
JavaScript let vs var Keyword
The let and var, both keywords are used to declare variables, but the main
difference is the scope of the declared variables.
A variable declared inside a block using var is accessible outside of the block
as it has a global scope but a variable declared using the let keyword has a
local scope. Let's see an example:
{
let amount = 2500; // block Scope
var withdraw = 2000; // global scope
}
document.write(withdraw) // accessible
document.write(amount) // not accessible
Output:
2000
Uncaught ReferenceError: amount is not defined
Syntax
Below we have the syntax to define a constant value in JavaScript.
const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]]
We don't have to use var or let keyword while using the const keyword. Also,
we can define a list or a simple value or a string etc as a constant.
26
Client-Side Scripting (Semester-V)
Output:
3.14
Let's try another example where we will try changing the value of the
constant and see if we allowed to reassign value or not.
{
const Pi = 3.14;
alert(Pi);
// Reassign value
Pi = 3.143;
alert(Pi);
}
Output:
3.14
Uncaught TypeError: Assignment to constant variable.
The scope of the variable defined using const keyword is same as that of a
variable declared using the let keyword. So, constant declared inside a
block will not accessible outside of that block. Let's take an example
and see:
{
const Pi = 3.14;
alert(Pi);
}
// outside block
alert(Pi); // error
Output:
3.14
Uncaught ReferenceError: Pi is not defined
Data Types
JavaScript provides different data types to hold different types of
values.
There are two types of data types in JavaScript:
o Primitive data type
27
Client-Side Scripting (Semester-V)
29
Client-Side Scripting (Semester-V)
<html>
<body>
<h1>JavaScript Array</h1>
<script>
var stringArray = ["one", "two", "three"];
var mixedArray = [1, "two", "three", 4];
document.write(stringArray+"<br>");
JavaScript Array
document.write( mixedArray);
one,two,three
</script> 1,two,three,4
</body>
</html>
Output:
Values/Literals
30
Client-Side Scripting (Semester-V)
They are types that can be assigned a single literal value such as the
number 5.7, or a string of characters such as "hello".
Types of Literals:
Array Literal
Integer Literal
Floating number Literal
Boolean Literal (include True and False)
Object Literal
String Literal
Array Literal:
an array literal is a list of expressions, each of which represents an
array element, enclosed in a pair of square brackets ' [ ] ‘ .
When an array is created using an array literal, it is initialized with
the specified values as its elements, and its length is set to the
number of arguments specified.
Creating an empty array :
var tv = [ ];
Creating an array with four elements.
var tv = [“LG", “Samsung", “Sony", “Panasonic"]
Comma in array literals:
In the following example, the length of the array is four, and tv[0] and
tv[2] are undefined.
var tv = [ , “Samsung“, , “Panasonic"]
This array has one empty element in the middle and two elements with
values.
( tv[0] is “LG", tv[1] is set to undefined, and tv[2] is “Sony")
Var tv = [“LG", ,“Sony", ]
Integer Literal:
An integer must have at least one digit (0-9).
• No comma or blanks are allowed within an integer.
• It does not contain any fractional part.
• It can be either positive or negative if no sign precedes it is
assumed to be positive.
In JavaScript, integers can be expressed in three different bases.
1. Decimal ( base 10)
Decimal numbers can be made with the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
and there will be no leading zeros.
Example: 123, -20, 12345
2. Hexadecimal ( base 16)
31
Client-Side Scripting (Semester-V)
Object Literal:
An object literal is zero or more pairs of comma-separated list of property
names and associated values, enclosed by a pair of curly braces.
In JavaScript an object literal is declared as follows:
var userObject = { }
32
Client-Side Scripting (Semester-V)
String Literal:
JavaScript has its own way to deal with string literals.
A string literal is zero or more characters, either enclosed in single
quotation (') marks or double quotation (") marks. You can also use +
operator to join strings.
The following are the examples of string literals:
string1 = "w3resource.com"
string1 = 'w3resource.com’
string1 = "1000“
In addition to ordinary characters, you can include special characters in
strings, as shown in the following.
string1 = "First line. \n Second line."
Special characters in JavaScript:
character Description
\’ Single quote
\” Double quote
\\ Backslash
\b Backspace
\f Form Feed
\n New line
\r Carriage
\t Horizontal tab
\v Vertical tab
Comments in JavaScript:
The JavaScript comments are meaningful way to deliver message.
It is used to add information about the code, warnings or suggestions so
that end user can easily interpret the code.
The JavaScript comment is ignored by the JavaScript engine i.e.
embedded in the browser.
There are two types of comments in JavaScript.
1. Single-line Comment
It is represented by double forward slashes (//).
It can be used before and after the statement.
Example,
<script>
33
Client-Side Scripting (Semester-V)
Code:
34
Client-Side Scripting (Semester-V)
<html>
<body>
<script type = "text/javascript">
var a = 33;
var b = 10;
var c = "Test";
document.write("a + b = ");
result = a + b;
document.write(result+"<br>");
document.write("a - b = ");
result = a - b;
document.write(result+"<br>");
document.write("a / b = ");
result = a / b;
document.write(result+"<br>");
document.write("a % b = ");
result = a % b;
document.write(result+"<br>");
document.write("a + b + c = ");
result = a + b + c;
document.write(result+"<br>");
a = ++a;
document.write("++a = ");
result = ++a;
document.write(result+"<br>");
b = --b;
document.write("--b = ");
result = --b;
document.write(result+"<br>");
</script>
35
Client-Side Scripting (Semester-V)
</body>
</html>
Output:
a + b = 43
a - b = 23
a / b = 3.3
a%b=3
a + b + c = 43Test
++a = 35
--b = 8
Code:
<html>
<body>
<script type = "text/javascript">
var a = 10;
var b = 20;
36
Client-Side Scripting (Semester-V)
document.write(result+"<br>");
37
Client-Side Scripting (Semester-V)
Code:
<html>
<body>
<script type="text/javascript">
38
Client-Side Scripting (Semester-V)
document.write(result+"<br>");
Output:
(a & b) => 2
(a | b) => 3
(a ^ b) => 1
(~b) => -4
(a << b) => 16
(a >> b) => 0
4) Logical Operator:
Code:
<html>
<body>
<script type = "text/javascript">
var a = true;
var b = false;
39
Client-Side Scripting (Semester-V)
40
Client-Side Scripting (Semester-V)
5) Assignment Operator:
Code:
<html>
<body>
<script type="text/javascript">
var a = 33;
var b = 10;
41
Client-Side Scripting (Semester-V)
result = (a -= b);
document.write(result+"<br>");
Output:
Value of a => (a = b) => 10
Value of a => (a += b) => 20
Value of a => (a -= b) => 10
Value of a => (a *= b) => 100
Value of a => (a /= b) => 10
Value of a => (a %= b) => 0
6) Special Operator:
Operato Description
r
42
Client-Side Scripting (Semester-V)
<html>
<body>
<script type = "text/javascript">
var a = 10;
var b = "Information";
var c= function(x)
{
return x*x;
}
. left-to-right
member
[]
44
Client-Side Scripting (Semester-V)
increment ++
decrement --
logical-not ! right-to-left
unary + + right-to-left
45
Client-Side Scripting (Semester-V)
>>
>>>
in in left to right
46
Client-Side Scripting (Semester-V)
Expression:
Any unit of code that can be evaluated to a value is an expression.
Since expressions produce values, they can appear anywhere in a
program where JavaScript expects a value such as the arguments of a
function invocation.
Types of Expression:
1. Primary Expression:
Primary expressions refer to stand alone expressions such as literal
values, certain keywords and variable values.
'hello world'; // A string literal
23; // A numeric literal
true; // Boolean value true
sum; // Value of variable sum
47
Client-Side Scripting (Semester-V)
Example:
emp.firstName;
emp[lastName];
48
Client-Side Scripting (Semester-V)
5. Invocation Expression
An invocation expression is JavaScript’s syntax for calling (or executing) a
function or method.
It starts with a function expression that identifies the function to be
called.
The function expression is followed by an open parenthesis, a comma-
separated list of zero or more argument expressions, and a close
parenthesis.
When an invocation expression is evaluated, the function expression is
evaluated first, and then the argument expressions are evaluated to
produce a list of argument values.
Example,
f(0) // f is the function expression; 0 is the argument expression
Math.max(x,y,z) // Math.max is the function; x, y and z are
the arguments.
49
Client-Side Scripting (Semester-V)
1) if statement:
Use if statement to specify a block of JavaScript code to be executed if a
condition is true.
Syntax:
if (condition)
{
//block of code to be executed if the condition
is true
}
Example:
<html>
<body>
<script>
if (new Date().getHours() < 18)
{
document.write("Good day!");
}
</script>
</body>
</html>
50
Client-Side Scripting (Semester-V)
if (condition)
{
// block of code to be executed if the condition is
true
} else
{
// block of code to be executed if the condition is
false
Example:
<html>
<body>
<script>
if (new Date().getHours() < 18)
{
document.write("Good day!");
}
else
{
document.write("Good Evening!");
}
</script>
</body>
</html>
Syntax:
51
Client-Side Scripting (Semester-V)
if (condition1)
{ // block of code to be executed if condition1 is true
}
else if (condition2)
{ // block of code to be executed if the condition1 is false and condition2
is true
}
else
{ // block of code to be executed if the condition1 is false and condition2
is false
Example:
<html>
<body>
<script>
var greeting;
var time = new Date().getHours();
if (time < 10)
{
greeting = "Good morning";
}
else if (time < 20)
{
greeting = "Good day";
}
else
{
greeting = "Good evening";
}
document.write(greeting);
</script>
</body>
</html>
4) The switch case Statement
The switch statement is used to perform different actions based on
different conditions. It is used to select one of many code blocks to be
executed.
Syntax:
switch(expression)
52
{
case x:
// code block
Client-Side Scripting (Semester-V)
Example:
<html>
<body>
<script>
var day;
switch (new Date().getDay())
{
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
53
Client-Side Scripting (Semester-V)
}
document.write("Today is " + day);
</script>
</body>
</html>
default keyword:
default keyword specifies the code to run if there is no case match.
The getDay() method returns the weekday as a number between 0 and 6.
If today is neither Saturday (6) nor Sunday (0), write a default message.
Example:
switch (new Date().getDay())
{
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "Looking forward to the Weekend";
}
Example:
<html>
<body>
<script>
// program for a simple calculator
var result;
54
Client-Side Scripting (Semester-V)
switch(operator)
{
case '+':
result = number1 + number2;
document.write(`${number1} + ${number2} = ${result}`);
break;
case '-':
result = number1 - number2;
document.write(`${number1} - ${number2} = ${result}`);
break;
case '*':
result = number1 * number2;
document.write(`${number1} * ${number2} = ${result}`);
break;
case '/':
result = number1 / number2;
document.write(`${number1} / ${number2} = ${result}`);
break;
default:
document.write('Invalid operator');
break;
}
</script>
</body>
</html>
Output:
55
Client-Side Scripting (Semester-V)
56
Client-Side Scripting (Semester-V)
1) for loop
The JavaScript for loop iterates the elements for the fixed number of
times. It should be used if number of iteration is known.
Syntax:
Example:
<script>
for (i=0; i<=10; i=i+2)
{
document.write(i + "<br/>")
}
</script>
2) do while Loop
loop is a variant of the while loop.
This loop will execute the code block once.
before checking if the condition is true, then it will repeat the loop as long as
the condition is true.
Syntax:
do
{
code to be executed
}
while (condition);
Example:
<script>
57
Client-Side Scripting (Semester-V)
var i=21;
do{
document.write(i +"<br/>");
i++;
}while (i<=25);
</script>
3) while loop
The JavaScript while loop loops through a block of code as long as a
specified condition is true.
Syntax:
while (condition)
{
Code to be executed
}
Example:
<script>
var i=11;
while (i<=20)
{
document.write(i + "<br/>");
i++;
}
</script>
4) for-in loop
The for/in statement loops through the properties of an object.
The block of code inside the loop will be executed once for each property.
Syntax:
Example:
58
Client-Side Scripting (Semester-V)
59
Client-Side Scripting (Semester-V)
In while loop, first it checks the In Do – While loop, first it executes the
condition and then executes the program and then checks the
program. condition.
The condition will come before the The condition will come after the
body. body.
If the condition is false, then it It runs at least once, even though the
terminates the loop. conditional is false.
break statement
break statement breaks the loop and continues executing the code after
the loop.
The break statement can also be used to jump out of a loop.
Example:
continue statement
Continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
Example:
Output:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 5
The number is 6
61
Client-Side Scripting (Semester-V)
Deleting properties:
The delete operator deletes a property from an object.
The delete operator deletes both the value of the property and the
property itself.
Syntax:
delete var_name.property;
<html>
<body>
<script>
var a={name:"Priti",age:35};
document.write(a.name+" "+a.age+"<br>");
delete a.age; //delete property
document.write(a.name+" "+a.age);
</script>
</body>
</html>
Output:
Priti 35
Priti undefined
Property getter and setter
Also known as Javascript assessors.
Getters and setters allow you to control how important variables are
accessed and updated in your code.
JavaScript can secure better data quality when using getters and setters.
62
Client-Side Scripting (Semester-V)
// Create an object:
var person = { firstName: "Chirag",
lastName : "Shetty",
fullName : function()
{
return this.firstName + " " + this.lastName;
}
};
document.write(person.fullName());
</script>
63
Client-Side Scripting (Semester-V)
Code: Getters and setters allow you to get and set properties via
methods.
<script>
var person = {
firstName: 'Chirag',
lastName: 'Shetty',
get fullName()
{
return this.firstName + ' ' + this.lastName;
}, Output:
set fullName (name)
Chirag Shetty
{
before using set
var words = name.split(' ');
fullname()
this.firstName = words[0];
YASH Desai
this.firstName = words[0].toUpperCase();
this.lastName = words[1];
}
}
document.write(person.fullName); //Getters and setters allow you to get
and set properties via methods.
document.write("<br>"+"before using set fullname()"+"<br>");
person.fullName = 'Yash Desai'; //Set a property using set
document.writeln(person.firstName); // Yash
document.write(person.lastName); // Desai
</script>
64