SlideShare a Scribd company logo
Lecture3


Data Type Conversion (변하게 하다, 전환하다)

To understand what is data type conversion, and why we need data type conversion, let
us take an example.


<html>
<body>
<script language="JavaScript" type="text/javascript">

var firstNumber = prompt("Enter the first number","");
var secondNumber = prompt("Enter the second number","");
var theTotal = firstNumber + secondNumber;
document.write(firstNumber + " added to " + secondNumber + " equals " +
   theTotal);

</script>
</body>
</html>

What is the output, if user enters firstNumber=2 and secondNumber=3, Output should
be 5.

But if you will execute above program, you will be disappointed (실망시키다) to find that
output is 23.

Why is output 23 instead of 5?
Ans: We know that function prompt () returns a string, and if we use '+' operator with
strings, they are concatenated (사슬같이 잇다 ).

To solve (<문제 등을> 풀다, 해석하다) this problem we will take help of data type
conversion. Data type conversion means convert data type.

What is the solution?
Solution is convert String to int or float. There are 2 conversion functions parseInt() and
parseFloat().

parseInt(): This functions takes a string and convert it to an integer.
For example:
            parseInt(“123”) = 123
            parseInt(“123Abc”) = 123

parseFloat(): This function take a string and convert it to a real number.

What is the output for the following?
1. parseInt(“ABC”)
2. parseInt(“ABC123”)

Find it and I will ask you in the class.
Now, modify (수정하다 ) the above code so that it gives correct output i.e 5.

<html>
<body>
<script language="JavaScript" type="text/javascript">

var firstNumber = prompt("Enter the first number","");
var secondNumber = prompt("Enter the second number","");
var theTotal = parseInt(firstNumber) + parseInt(secondNumber);
document.write(firstNumber + " added to " + secondNumber + " equals " +
   theTotal);

</script>
</body>
</html>



What is NaN?

If you write document.write(parseInt(“abc”)), output will be NaN. NaN means Not a
Number.

If you use parseInt() or parseFloat() and input argument is a string that is empty or
doesn't start with a number, output is NaN.

There is a function in Javascript isNaN(x), which checks whether 'x' is NaN or not.
For example:
            isNaN(“123”) returns false
            isNaN(“ABC”) returns true
            isNaN(“123ABC”) returns true


So, Before converting any String to a number we can check beforehand (미리) whether
string consist ((부분·요소로) 되어[이루어져] 있다 ) of numbers or not. If string consist of
numbers, we can convert it otherwise ([접속사적으로] 만약 그렇지 않으면) display an error
message.

Below is a program which checks for user input. If user input is not valid, we generate
an error message.

<html>
<body>
<script language="JavaScript" type="text/javascript">

var firstNumber = prompt("Enter the first number","");
var secondNumber = prompt("Enter the second number","");
var ifn,isn;
if (isNaN(firstNumber))
      alert("this is Not a Number");
else
      ifn = parseInt(firstNumber);
if (isNaN(secondNumber))
      alert("this is not a number");
else
      isn = parseInt(secondNumber);
var theTotal = ifn+ isn;
document.write(firstNumber + " added to " + secondNumber + " equals " +
   theTotal);
</script>
</body>
</html>

Multi Dimensional Array

In my previous lecture, I taught you about 1-dim arrays. 1-dim arrays are those that
have only one index.
Like 1-dim arrays, there are 2-dim, 3-dim and multi-dim arrays.

2-dim arrays are those that have 2-dimensions.

First answer this question, What is the output of following code

<html>
<body>
<script language="JavaScript" type="text/javascript">
var a = new Array("a",2,"b",3,"c",4);
for (i=0;i<a.length;i++)
        document.write("a["+i+"] = "+a[i]+"<br>");
</script>
</body>
</html>

Two things are worth (…의 가치가 있는 ) notice (통지, 통보) in the above program
1. Unlike (같지 않은, 다른, 닮지 않은 ) arrays in other languages, arrays in Javascript can
   store values of different data types. For example:

              var a = new Array("a",2,"b",3,"c",4);

2. I told in my previous lecture also, that new keyword create an object. This means, 'a'
   is an object of Array.
   And length is a variable of Array Object in Javascript, So a.length returns length of
   array 'a'.

To understand 2-dim arrays, Let us take an example.

Suppose, we want to store a company's employee information in an array. Each
employee has
-name
-age
-address

One way to do this, is store the information in a 1-dim array sequentially (잇달아 일어나
는, 연속하는, 순차적인 ). For example:

Index               Data Stored
0                   Name1
1                   Age1
Index              Data Stored
2                  Address1
3                  Name2
4                  Age2
5                  Address2
6                  Name3
7                  Age3
8                  Address3

var employee = new
Array(“Huh”,23,”suwon”,”cho”,30,”hwaseong”,”Chong”,28,”Guro”,”chae”,40,”bundang”);

or

employee[0] = “huh”;
employee[1] = 23;
employee[2] = “suwon”;

employee[3] = “cho”;
employee[4] = 30;
employee[5] = “hwaseong”;

employee[6] = “chong”;
employee[7] = 28;
employee[8] = “guro”;

employee[9] = “chae”;
employee[10] = 40;
employee[11] = “bundang”;

employee is a 1-dim array, employee[0], employee[1], employee[2] stores information
about one employee.
empoyee[3], employee[4], employee[5] stores information about 2nd employee.

2nd way of doing this, is store the information in a 2-dim arrays.

Index          0              1           2
0              Name1          Name2       Name3
1              Age1           Age2        Age3
2              Address1       Address2    Address3

var employee = new Array(); // employee is        an array
employee[0] = new Array(); // employee[0]         is also an array
employee[1] = new Array(); // employee[1]         is also an array
employee[2] = new Array(); // employee[2]         is also an array
The first index (0) belongs ((…의) 소유물이다 ) to the employee array; the second index
(0) belongs to the employee[0] array.

employee[0][0] = “huh”;
employee[0][1] = “23”;
employee[0][2] = “suwon”;

employee[1][0] = “cho”;
employee[1][1] = “30”;
employee[1][2] = “hwaseong”;

employee[2][0] = “chong”;
employee[2][1] = “28”;
employee[2][2] = “guro”;

employee[3][0] = “chae”;
employee[3][1] = “40”;
employee[3][2] = “bundang”;

Here is the complete program

<html>
<body>

<script language="JavaScript" type="text/javascript">

var employee = new Array();

employee[0][0] = “huh”;
employee[0][1] = “23”;
employee[0][2] = “suwon”;
employee[1] = new Array();
employee[1][0] = “cho”;
employee[1][1] = “30”;
employee[1][2] = “hwaseong”;


employee[2] = new Array();
employee[2][0] = “chong”;
employee[2][1] = “28”;
employee[2][2] = “guro”;

document.write("Name : " + employee[1][0] + "<br>");
document.write("Age : " + employee[1][1] + "<br>");
document.write("Address : " + employee[1][2]);

</script>

</body>
</html>

Answer these?

var myArray = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim?
myArray[0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim?
myArray[0][0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim?
myArray[0][0][0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim?
myArray[0][0][0][0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim?

Javascript-An Object based language

In this topic, we will look at most important aspect of Javascript programing 'Objects'.
Javascript itself consist of Objects and browser also made of collection of
object.

For example:
One Javascript object that you have used is Array.
You have also used 2 objects document and window, these are objects provided by
browser.
We can create our own objects.

A brief introduction about objects

Those who have been doing programming in C++ or Java already know about objects, I
still fell the need to give an introduction about objects for those who are first time using
it.

To understand what are objects, we take an example. We will take an example of car.

How do we define a car? We define a car as a blue car with 4 wheel drive, automatic
gear, power steering, etc. Color, 4 wheel, automatic gear, 4 wheel drive, power steering
are properties of a car. If I make an object of type Car, color, automatic gear, 4 wheel
drive, power steering will be attributes or property (재산, 자산 ) of object. In Javascript,
we represent attributes as properties for an object.

How do we use a car? We turn in the key, press the pedal, change the gear, and then
move the steering. This can also be defined as behavior (행동, 거동, 행실, 품행 ) of car. In
Javascript, we represent the behavior as methods or functions of an object.

We can pass arguments to a function, for example if gear() is a function, then one of its
argument can be gear number, gear 1 or 2 or 3 or 4 or reverse.
Similarly, speed() function can return me the speed of the car.
Or OilLeft() function can return the amount of oil left in the car.

If we want to define an object for car, we need to defined attributes and functions for
the car object. In Object based programming, we try to model real life things by an
Object. And objects are defined by their attributes and functions.

Basic definition of an Object is “A thing with attributes and functions”.

Take an example, We used array.length which returns length of the array, now array is
an object and length is an attribute of array object. Length attribute of array object,
returns number of elements stored in the array.
Let us do to anatomy (해부;해부학;해부술 ) of Array object.

We create an array by the statement
var num = new Array(7, 6, 5, 4, 3, 2, 1, 0, 8, 9);

'num' is a variable name. In the above example, right hand side of the statement create




an array in memory.

Starting address of the array is 100. 'num' stores only the starting address of array i.e.
'num' stores value 100.

new is a keyword in Javascript for creating object. In the above example, we are
creating an object of type Array. Object is created in memory and its starting address is
100 which is stored in variable 'num'.

Let us take another example of using Date Object

var d = new Date(“1 Jan 2000”);

'd' is a variable which can store an integer, character, string etc, in the above example
variable 'd' stores address of an object of type Date.

'new' is a keyword that creates an object. In the above example we are creating an
object of type Date. The object of type Date takes some space in memory and has a
starting address. 'd' stores starting address of object of type Date.

Let us try to understand more about objects and variables.
I am starting with an example of simple variables.

1.   var a = 10;
2.   var b = a;
3.   document.write(a);   // Output is 10
4.   document.write(b);   // Output is 10
5.   b = 20;
6.   document.write(a);   // Output is 10
7.   document.write(b);   // Output is 20

a and b are 2 different variables.
Line 1, variable a stores value 10.
Line 2, variable b also stores the value value stored in a (10). So b also stores value 10.
Line 3 output is 10 and line 4 output is 20.
Line 5, we change the value stored in b. New value stored in b is 20 now. But value
stored in a does not change which is still 10.
Line 6 output is 10, because value stored in a does not change.
Line 7 output is 20, because value stored in b changes from 10 to 20.

The above was a simple example using variables. Next we take an example using
objects.

   1. var a = new Array(7, 6, 5);




   2. var b = a;




3. document.write(a[0] + “ ”+ a[1]+ “ ”+ a[2]); // output is 7 6 5
4. document.write(b[0] + “ ”+ b[1]+ “ ”+ b[2]); // output is yeh
5. b[1] = 3;




6. document.write(a[0] + “ ”+ a[1]+ “ ”+ a[2]); // output is 7 3 5
7. document.write(b[0] + “ ”+ b[1]+ “ ”+ b[2]);// output is 7 3 5

Line 1, creates an object of type array and stores the address of object in a. Suppose
Object of type array has starting address 100, then a stores 100.

Line 2, variable b gets the value stores in a, which is 100. i.e. B = 100.

Line 3, output is 7 6 5.

Line 4, output is 7 6 5, because b also contain the address 100 which is same as
address stored in a.
Line 5. We change the value if b[1] to 2, because a and b both contain the same address
b[1] and a[1] refer to same location of memory.

Line 6 output is 7 3 5 and line 7 output is 7 3 5.

Using an Object Properties and Methods

Every object provide (지급하다) some methods and properties. For example, we can find the length of the array,
For example:

var a = new Array( 7 , 6 , 5);
var len = a.length; // output is 3

length is a property of Array object. It returns the length of the array.
Address of Array object is stored in the variable a. So a can access all the properties of Array object.

a . name of property

Simillary,
a . name of function(), here function can be replaced by actual function name.
For example
Date d = new Date(“15 Aug 1953”)
dd = d.getDate(); // dd = 31

getDate() is a function of Date Object.


Primitives and Objects

You should now have a good idea about the difference between primitive (원시의, 초기의;
태고의, 옛날의 ) data, such as numbers and strings, and object data, such as Dates and
Arrays.
We have string, number and boolean data types. In fact there are String, Number, and
Boolean objects corresponding (유사한 ) to the three string, number, and Boolean
primitive data types. For example, to create a String object containing the text "I'm a
String object," we can use
var myString = new String("I'm a String object");

The String object has the length property just as the Array object does. This returns the
number of characters in the String object. For example
var lengthOfString = myString.length;

would store the data 19 in the variable lengthOfString.
But what if we had declared a primitive string called mySecondString holding the text
"I'm a _primitive string" like this:
var mySecondString = "I'm a primitive string";

and wanted to know how many characters could be found in this primitive string?
So, for our primitive string mySecondString, we can use the length property of the
String object to find out the number of characters it contains. For example
var lengthOfSecondString = mySecondString.length;

would store the data 22 in the variable lengthOfSecondString
If we declare a primitive string and then treat it as an object, such as by trying to access
one of its methods or properties, JavaScript would know that the operation we're trying
to do won't work if it's a primitive string. It will only work if it's an object; for example, it
would be valid if it were a String object. In this case, JavaScript converts the plain
text string into a temporary String object, just for that operation.

More Related Content

What's hot (20)

Functional es6
Functional es6Functional es6
Functional es6
Natalia Zaslavskaya
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Alf Kristian Støyle
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
Reem Alattas
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리
용 최
 
Lodash js
Lodash jsLodash js
Lodash js
LearningTech
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
timourian
 
Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2
Hariz Mustafa
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
RajKumar Rampelli
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
Frayosh Wadia
 
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Fredrik Vraalsen
 
Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2
Ed Charbeneau
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
Abed Bukhari
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
Pedro Rodrigues
 
Objective-C for Java developers
Objective-C for Java developersObjective-C for Java developers
Objective-C for Java developers
Fábio Bernardo
 
Chaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreChaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscore
Nicolas Carlo
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
Groovy
GroovyGroovy
Groovy
congcong wang
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
Mårten Rånge
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
Jesper Kamstrup Linnet
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
Josh Doyle
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
Reem Alattas
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리
용 최
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
timourian
 
Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2
Hariz Mustafa
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
RajKumar Rampelli
 
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Fredrik Vraalsen
 
Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2
Ed Charbeneau
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
Abed Bukhari
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
Pedro Rodrigues
 
Objective-C for Java developers
Objective-C for Java developersObjective-C for Java developers
Objective-C for Java developers
Fábio Bernardo
 
Chaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreChaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscore
Nicolas Carlo
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
Jesper Kamstrup Linnet
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
Mårten Rånge
 

Viewers also liked (19)

Resolution
ResolutionResolution
Resolution
H K
 
Appendix css
Appendix cssAppendix css
Appendix css
H K
 
Java script advanced frame
Java script advanced frameJava script advanced frame
Java script advanced frame
H K
 
Assignment sw solution
Assignment sw solutionAssignment sw solution
Assignment sw solution
H K
 
Html dom part-ii
Html dom part-iiHtml dom part-ii
Html dom part-ii
H K
 
Solution3
Solution3Solution3
Solution3
H K
 
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basics
H K
 
Ch04
Ch04Ch04
Ch04
H K
 
Html basics 4 anchor list
Html basics 4 anchor listHtml basics 4 anchor list
Html basics 4 anchor list
H K
 
Assignment sw
Assignment swAssignment sw
Assignment sw
H K
 
Html basics 8 frame
Html basics 8 frameHtml basics 8 frame
Html basics 8 frame
H K
 
Ip protocol tedting
Ip protocol tedtingIp protocol tedting
Ip protocol tedting
H K
 
Lecture3layered archi
Lecture3layered archiLecture3layered archi
Lecture3layered archi
H K
 
Week32
Week32Week32
Week32
H K
 
Lecture20 tcp
Lecture20 tcpLecture20 tcp
Lecture20 tcp
H K
 
Week4142
Week4142Week4142
Week4142
H K
 
Css 2
Css 2Css 2
Css 2
H K
 
Logic
LogicLogic
Logic
H K
 
Proof
ProofProof
Proof
H K
 
Resolution
ResolutionResolution
Resolution
H K
 
Appendix css
Appendix cssAppendix css
Appendix css
H K
 
Java script advanced frame
Java script advanced frameJava script advanced frame
Java script advanced frame
H K
 
Assignment sw solution
Assignment sw solutionAssignment sw solution
Assignment sw solution
H K
 
Html dom part-ii
Html dom part-iiHtml dom part-ii
Html dom part-ii
H K
 
Solution3
Solution3Solution3
Solution3
H K
 
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basics
H K
 
Ch04
Ch04Ch04
Ch04
H K
 
Html basics 4 anchor list
Html basics 4 anchor listHtml basics 4 anchor list
Html basics 4 anchor list
H K
 
Assignment sw
Assignment swAssignment sw
Assignment sw
H K
 
Html basics 8 frame
Html basics 8 frameHtml basics 8 frame
Html basics 8 frame
H K
 
Ip protocol tedting
Ip protocol tedtingIp protocol tedting
Ip protocol tedting
H K
 
Lecture3layered archi
Lecture3layered archiLecture3layered archi
Lecture3layered archi
H K
 
Week32
Week32Week32
Week32
H K
 
Lecture20 tcp
Lecture20 tcpLecture20 tcp
Lecture20 tcp
H K
 
Week4142
Week4142Week4142
Week4142
H K
 
Css 2
Css 2Css 2
Css 2
H K
 
Logic
LogicLogic
Logic
H K
 
Proof
ProofProof
Proof
H K
 
Ad

Similar to Java script objects 1 (20)

Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
Javascript
JavascriptJavascript
Javascript
Manav Prasad
 
1-JAVA SCRIPT. servere-side applications vs client side applications
1-JAVA SCRIPT. servere-side applications vs client side applications1-JAVA SCRIPT. servere-side applications vs client side applications
1-JAVA SCRIPT. servere-side applications vs client side applications
surajshreyans
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
Varun C M
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
MD Sayem Ahmed
 
CSC PPT 13.pptx
CSC PPT 13.pptxCSC PPT 13.pptx
CSC PPT 13.pptx
DrRavneetSingh
 
Learn java script
Learn java scriptLearn java script
Learn java script
Mahmoud Asadi
 
Js objects
Js objectsJs objects
Js objects
Charles Russell
 
Unit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptxUnit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptx
meghana092
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
Javascript
JavascriptJavascript
Javascript
20261A05H0SRIKAKULAS
 
JavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentationJavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentation
M Sajid R
 
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdfjavascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
AlexShon3
 
Fewd week5 slides
Fewd week5 slidesFewd week5 slides
Fewd week5 slides
William Myers
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
KennyPratheepKumar
 
Storage in programming
Storage in programmingStorage in programming
Storage in programming
Codewizacademy
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
JavaScripts & jQuery
JavaScripts & jQueryJavaScripts & jQuery
JavaScripts & jQuery
Asanka Indrajith
 
javascript-Array.ppsx
javascript-Array.ppsxjavascript-Array.ppsx
javascript-Array.ppsx
VedantSaraf9
 
Unit II- Java Script, DOM JQuery (2).pptx
Unit II- Java Script, DOM JQuery (2).pptxUnit II- Java Script, DOM JQuery (2).pptx
Unit II- Java Script, DOM JQuery (2).pptx
SiddheshMhatre21
 
1-JAVA SCRIPT. servere-side applications vs client side applications
1-JAVA SCRIPT. servere-side applications vs client side applications1-JAVA SCRIPT. servere-side applications vs client side applications
1-JAVA SCRIPT. servere-side applications vs client side applications
surajshreyans
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
Varun C M
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
MD Sayem Ahmed
 
Unit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptxUnit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptx
meghana092
 
JavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentationJavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentation
M Sajid R
 
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdfjavascript-variablesanddatatypes-130218094831-phpapp01.pdf
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
AlexShon3
 
Storage in programming
Storage in programmingStorage in programming
Storage in programming
Codewizacademy
 
javascript-Array.ppsx
javascript-Array.ppsxjavascript-Array.ppsx
javascript-Array.ppsx
VedantSaraf9
 
Unit II- Java Script, DOM JQuery (2).pptx
Unit II- Java Script, DOM JQuery (2).pptxUnit II- Java Script, DOM JQuery (2).pptx
Unit II- Java Script, DOM JQuery (2).pptx
SiddheshMhatre21
 
Ad

More from H K (20)

Assignment4
Assignment4Assignment4
Assignment4
H K
 
Assignment3
Assignment3Assignment3
Assignment3
H K
 
Induction
InductionInduction
Induction
H K
 
Solution2
Solution2Solution2
Solution2
H K
 
Mid-
Mid-Mid-
Mid-
H K
 
Assignment4
Assignment4Assignment4
Assignment4
H K
 
Assignment4
Assignment4Assignment4
Assignment4
H K
 
Dm assignment3
Dm assignment3Dm assignment3
Dm assignment3
H K
 
Assignment description
Assignment descriptionAssignment description
Assignment description
H K
 
Dm assignment2
Dm assignment2Dm assignment2
Dm assignment2
H K
 
Set
SetSet
Set
H K
 
Dm assignment1
Dm assignment1Dm assignment1
Dm assignment1
H K
 
Introduction
IntroductionIntroduction
Introduction
H K
 
Assignment 2 sol
Assignment 2 solAssignment 2 sol
Assignment 2 sol
H K
 
Violinphoenix
ViolinphoenixViolinphoenix
Violinphoenix
H K
 
Ie project
Ie projectIe project
Ie project
H K
 
Assignment cn subnetting
Assignment cn subnettingAssignment cn subnetting
Assignment cn subnetting
H K
 
Assignment uplaodfile
Assignment uplaodfileAssignment uplaodfile
Assignment uplaodfile
H K
 
Assignment sw
Assignment swAssignment sw
Assignment sw
H K
 
Assignment cn tl
Assignment cn tlAssignment cn tl
Assignment cn tl
H K
 
Assignment4
Assignment4Assignment4
Assignment4
H K
 
Assignment3
Assignment3Assignment3
Assignment3
H K
 
Induction
InductionInduction
Induction
H K
 
Solution2
Solution2Solution2
Solution2
H K
 
Mid-
Mid-Mid-
Mid-
H K
 
Assignment4
Assignment4Assignment4
Assignment4
H K
 
Assignment4
Assignment4Assignment4
Assignment4
H K
 
Dm assignment3
Dm assignment3Dm assignment3
Dm assignment3
H K
 
Assignment description
Assignment descriptionAssignment description
Assignment description
H K
 
Dm assignment2
Dm assignment2Dm assignment2
Dm assignment2
H K
 
Set
SetSet
Set
H K
 
Dm assignment1
Dm assignment1Dm assignment1
Dm assignment1
H K
 
Introduction
IntroductionIntroduction
Introduction
H K
 
Assignment 2 sol
Assignment 2 solAssignment 2 sol
Assignment 2 sol
H K
 
Violinphoenix
ViolinphoenixViolinphoenix
Violinphoenix
H K
 
Ie project
Ie projectIe project
Ie project
H K
 
Assignment cn subnetting
Assignment cn subnettingAssignment cn subnetting
Assignment cn subnetting
H K
 
Assignment uplaodfile
Assignment uplaodfileAssignment uplaodfile
Assignment uplaodfile
H K
 
Assignment sw
Assignment swAssignment sw
Assignment sw
H K
 
Assignment cn tl
Assignment cn tlAssignment cn tl
Assignment cn tl
H K
 

Recently uploaded (20)

Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 

Java script objects 1

  • 1. Lecture3 Data Type Conversion (변하게 하다, 전환하다) To understand what is data type conversion, and why we need data type conversion, let us take an example. <html> <body> <script language="JavaScript" type="text/javascript"> var firstNumber = prompt("Enter the first number",""); var secondNumber = prompt("Enter the second number",""); var theTotal = firstNumber + secondNumber; document.write(firstNumber + " added to " + secondNumber + " equals " + theTotal); </script> </body> </html> What is the output, if user enters firstNumber=2 and secondNumber=3, Output should be 5. But if you will execute above program, you will be disappointed (실망시키다) to find that output is 23. Why is output 23 instead of 5? Ans: We know that function prompt () returns a string, and if we use '+' operator with strings, they are concatenated (사슬같이 잇다 ). To solve (<문제 등을> 풀다, 해석하다) this problem we will take help of data type conversion. Data type conversion means convert data type. What is the solution? Solution is convert String to int or float. There are 2 conversion functions parseInt() and parseFloat(). parseInt(): This functions takes a string and convert it to an integer. For example: parseInt(“123”) = 123 parseInt(“123Abc”) = 123 parseFloat(): This function take a string and convert it to a real number. What is the output for the following? 1. parseInt(“ABC”) 2. parseInt(“ABC123”) Find it and I will ask you in the class.
  • 2. Now, modify (수정하다 ) the above code so that it gives correct output i.e 5. <html> <body> <script language="JavaScript" type="text/javascript"> var firstNumber = prompt("Enter the first number",""); var secondNumber = prompt("Enter the second number",""); var theTotal = parseInt(firstNumber) + parseInt(secondNumber); document.write(firstNumber + " added to " + secondNumber + " equals " + theTotal); </script> </body> </html> What is NaN? If you write document.write(parseInt(“abc”)), output will be NaN. NaN means Not a Number. If you use parseInt() or parseFloat() and input argument is a string that is empty or doesn't start with a number, output is NaN. There is a function in Javascript isNaN(x), which checks whether 'x' is NaN or not. For example: isNaN(“123”) returns false isNaN(“ABC”) returns true isNaN(“123ABC”) returns true So, Before converting any String to a number we can check beforehand (미리) whether string consist ((부분·요소로) 되어[이루어져] 있다 ) of numbers or not. If string consist of numbers, we can convert it otherwise ([접속사적으로] 만약 그렇지 않으면) display an error message. Below is a program which checks for user input. If user input is not valid, we generate an error message. <html> <body> <script language="JavaScript" type="text/javascript"> var firstNumber = prompt("Enter the first number",""); var secondNumber = prompt("Enter the second number",""); var ifn,isn; if (isNaN(firstNumber)) alert("this is Not a Number"); else ifn = parseInt(firstNumber); if (isNaN(secondNumber)) alert("this is not a number"); else isn = parseInt(secondNumber); var theTotal = ifn+ isn; document.write(firstNumber + " added to " + secondNumber + " equals " + theTotal);
  • 3. </script> </body> </html> Multi Dimensional Array In my previous lecture, I taught you about 1-dim arrays. 1-dim arrays are those that have only one index. Like 1-dim arrays, there are 2-dim, 3-dim and multi-dim arrays. 2-dim arrays are those that have 2-dimensions. First answer this question, What is the output of following code <html> <body> <script language="JavaScript" type="text/javascript"> var a = new Array("a",2,"b",3,"c",4); for (i=0;i<a.length;i++) document.write("a["+i+"] = "+a[i]+"<br>"); </script> </body> </html> Two things are worth (…의 가치가 있는 ) notice (통지, 통보) in the above program 1. Unlike (같지 않은, 다른, 닮지 않은 ) arrays in other languages, arrays in Javascript can store values of different data types. For example: var a = new Array("a",2,"b",3,"c",4); 2. I told in my previous lecture also, that new keyword create an object. This means, 'a' is an object of Array. And length is a variable of Array Object in Javascript, So a.length returns length of array 'a'. To understand 2-dim arrays, Let us take an example. Suppose, we want to store a company's employee information in an array. Each employee has -name -age -address One way to do this, is store the information in a 1-dim array sequentially (잇달아 일어나 는, 연속하는, 순차적인 ). For example: Index Data Stored 0 Name1 1 Age1
  • 4. Index Data Stored 2 Address1 3 Name2 4 Age2 5 Address2 6 Name3 7 Age3 8 Address3 var employee = new Array(“Huh”,23,”suwon”,”cho”,30,”hwaseong”,”Chong”,28,”Guro”,”chae”,40,”bundang”); or employee[0] = “huh”; employee[1] = 23; employee[2] = “suwon”; employee[3] = “cho”; employee[4] = 30; employee[5] = “hwaseong”; employee[6] = “chong”; employee[7] = 28; employee[8] = “guro”; employee[9] = “chae”; employee[10] = 40; employee[11] = “bundang”; employee is a 1-dim array, employee[0], employee[1], employee[2] stores information about one employee. empoyee[3], employee[4], employee[5] stores information about 2nd employee. 2nd way of doing this, is store the information in a 2-dim arrays. Index 0 1 2 0 Name1 Name2 Name3 1 Age1 Age2 Age3 2 Address1 Address2 Address3 var employee = new Array(); // employee is an array employee[0] = new Array(); // employee[0] is also an array employee[1] = new Array(); // employee[1] is also an array employee[2] = new Array(); // employee[2] is also an array
  • 5. The first index (0) belongs ((…의) 소유물이다 ) to the employee array; the second index (0) belongs to the employee[0] array. employee[0][0] = “huh”; employee[0][1] = “23”; employee[0][2] = “suwon”; employee[1][0] = “cho”; employee[1][1] = “30”; employee[1][2] = “hwaseong”; employee[2][0] = “chong”; employee[2][1] = “28”; employee[2][2] = “guro”; employee[3][0] = “chae”; employee[3][1] = “40”; employee[3][2] = “bundang”; Here is the complete program <html> <body> <script language="JavaScript" type="text/javascript"> var employee = new Array(); employee[0][0] = “huh”; employee[0][1] = “23”; employee[0][2] = “suwon”; employee[1] = new Array(); employee[1][0] = “cho”; employee[1][1] = “30”; employee[1][2] = “hwaseong”; employee[2] = new Array(); employee[2][0] = “chong”; employee[2][1] = “28”; employee[2][2] = “guro”; document.write("Name : " + employee[1][0] + "<br>"); document.write("Age : " + employee[1][1] + "<br>"); document.write("Address : " + employee[1][2]); </script> </body> </html> Answer these? var myArray = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim? myArray[0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim?
  • 6. myArray[0][0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim? myArray[0][0][0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim? myArray[0][0][0][0] = new Array(); Is it 1-dim or 2-dim array or 3-dim or 4-dim or 5-dim? Javascript-An Object based language In this topic, we will look at most important aspect of Javascript programing 'Objects'. Javascript itself consist of Objects and browser also made of collection of object. For example: One Javascript object that you have used is Array. You have also used 2 objects document and window, these are objects provided by browser. We can create our own objects. A brief introduction about objects Those who have been doing programming in C++ or Java already know about objects, I still fell the need to give an introduction about objects for those who are first time using it. To understand what are objects, we take an example. We will take an example of car. How do we define a car? We define a car as a blue car with 4 wheel drive, automatic gear, power steering, etc. Color, 4 wheel, automatic gear, 4 wheel drive, power steering are properties of a car. If I make an object of type Car, color, automatic gear, 4 wheel drive, power steering will be attributes or property (재산, 자산 ) of object. In Javascript, we represent attributes as properties for an object. How do we use a car? We turn in the key, press the pedal, change the gear, and then move the steering. This can also be defined as behavior (행동, 거동, 행실, 품행 ) of car. In Javascript, we represent the behavior as methods or functions of an object. We can pass arguments to a function, for example if gear() is a function, then one of its argument can be gear number, gear 1 or 2 or 3 or 4 or reverse. Similarly, speed() function can return me the speed of the car. Or OilLeft() function can return the amount of oil left in the car. If we want to define an object for car, we need to defined attributes and functions for the car object. In Object based programming, we try to model real life things by an Object. And objects are defined by their attributes and functions. Basic definition of an Object is “A thing with attributes and functions”. Take an example, We used array.length which returns length of the array, now array is an object and length is an attribute of array object. Length attribute of array object, returns number of elements stored in the array.
  • 7. Let us do to anatomy (해부;해부학;해부술 ) of Array object. We create an array by the statement var num = new Array(7, 6, 5, 4, 3, 2, 1, 0, 8, 9); 'num' is a variable name. In the above example, right hand side of the statement create an array in memory. Starting address of the array is 100. 'num' stores only the starting address of array i.e. 'num' stores value 100. new is a keyword in Javascript for creating object. In the above example, we are creating an object of type Array. Object is created in memory and its starting address is 100 which is stored in variable 'num'. Let us take another example of using Date Object var d = new Date(“1 Jan 2000”); 'd' is a variable which can store an integer, character, string etc, in the above example variable 'd' stores address of an object of type Date. 'new' is a keyword that creates an object. In the above example we are creating an object of type Date. The object of type Date takes some space in memory and has a starting address. 'd' stores starting address of object of type Date. Let us try to understand more about objects and variables. I am starting with an example of simple variables. 1. var a = 10; 2. var b = a; 3. document.write(a); // Output is 10 4. document.write(b); // Output is 10 5. b = 20; 6. document.write(a); // Output is 10 7. document.write(b); // Output is 20 a and b are 2 different variables. Line 1, variable a stores value 10. Line 2, variable b also stores the value value stored in a (10). So b also stores value 10. Line 3 output is 10 and line 4 output is 20.
  • 8. Line 5, we change the value stored in b. New value stored in b is 20 now. But value stored in a does not change which is still 10. Line 6 output is 10, because value stored in a does not change. Line 7 output is 20, because value stored in b changes from 10 to 20. The above was a simple example using variables. Next we take an example using objects. 1. var a = new Array(7, 6, 5); 2. var b = a; 3. document.write(a[0] + “ ”+ a[1]+ “ ”+ a[2]); // output is 7 6 5 4. document.write(b[0] + “ ”+ b[1]+ “ ”+ b[2]); // output is yeh 5. b[1] = 3; 6. document.write(a[0] + “ ”+ a[1]+ “ ”+ a[2]); // output is 7 3 5 7. document.write(b[0] + “ ”+ b[1]+ “ ”+ b[2]);// output is 7 3 5 Line 1, creates an object of type array and stores the address of object in a. Suppose Object of type array has starting address 100, then a stores 100. Line 2, variable b gets the value stores in a, which is 100. i.e. B = 100. Line 3, output is 7 6 5. Line 4, output is 7 6 5, because b also contain the address 100 which is same as address stored in a.
  • 9. Line 5. We change the value if b[1] to 2, because a and b both contain the same address b[1] and a[1] refer to same location of memory. Line 6 output is 7 3 5 and line 7 output is 7 3 5. Using an Object Properties and Methods Every object provide (지급하다) some methods and properties. For example, we can find the length of the array, For example: var a = new Array( 7 , 6 , 5); var len = a.length; // output is 3 length is a property of Array object. It returns the length of the array. Address of Array object is stored in the variable a. So a can access all the properties of Array object. a . name of property Simillary, a . name of function(), here function can be replaced by actual function name. For example Date d = new Date(“15 Aug 1953”) dd = d.getDate(); // dd = 31 getDate() is a function of Date Object. Primitives and Objects You should now have a good idea about the difference between primitive (원시의, 초기의; 태고의, 옛날의 ) data, such as numbers and strings, and object data, such as Dates and Arrays. We have string, number and boolean data types. In fact there are String, Number, and Boolean objects corresponding (유사한 ) to the three string, number, and Boolean primitive data types. For example, to create a String object containing the text "I'm a String object," we can use var myString = new String("I'm a String object"); The String object has the length property just as the Array object does. This returns the number of characters in the String object. For example var lengthOfString = myString.length; would store the data 19 in the variable lengthOfString. But what if we had declared a primitive string called mySecondString holding the text "I'm a _primitive string" like this: var mySecondString = "I'm a primitive string"; and wanted to know how many characters could be found in this primitive string?
  • 10. So, for our primitive string mySecondString, we can use the length property of the String object to find out the number of characters it contains. For example var lengthOfSecondString = mySecondString.length; would store the data 22 in the variable lengthOfSecondString If we declare a primitive string and then treat it as an object, such as by trying to access one of its methods or properties, JavaScript would know that the operation we're trying to do won't work if it's a primitive string. It will only work if it's an object; for example, it would be valid if it were a String object. In this case, JavaScript converts the plain text string into a temporary String object, just for that operation.