27
06/15/2022 Developed By: Saif Ullah Dar 1 SESSION NO 3 JAVA SCRIPT

Java script session 3

Embed Size (px)

Citation preview

Page 1: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

1S E S S I O N N O 3

JAVA SCRIPT

Page 2: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

1) Java Script Data Types.2) Primitive Data Types.3) Composite Data Types.4) Trivial Data Types.5) Java Script Variables.6) Rules for Declaration of Variables7) Java Script Keywords.8) Scope of Variables.9) Examples 10)Quiz

SESSION OBJECTIVES

2

Page 3: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• Data is the information and Data Types means the ways in which we can retrieve the data.• There are two Main types of the Data Types in

Java Script Language1. Primitive Data Types2. Composite Data Types

JAVA SCRIPT DATA TYPES

3

Page 4: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar 4

JAVA SCRIPT DATA TYPES

Page 5: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• JavaScript has three “primitive” types: number, string, and Boolean

• Numbers are always stored as floating-point values• Hexadecimal numbers begin with 0x• Some platforms treat 0123 as octal, others treat it as

decimal• Strings may be enclosed in single quotes or

double quotes• Strings can contains \n (newline), \" (double quote), etc.

• Booleans are either true or false• 0, "0", empty strings, undefined, null, and NaN are false ,

other values are true

PRIMITIVE DATA TYPES

5

Page 6: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

PRIMITIVE DATA TYPES

6

Page 7: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• A composite data type stores a collection of multiple related values, unlike primitive data types.• JavaScript supports a composite data type known

as objects, Functions, Arrays• This will be discussed in the next Chapter in

details.

COMPOSITE DATA TYPES

7

JS

Page 8: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• Java does not make a distinction between integer values and floating-point values. All numbers in JavaScript are represented as floating-point values. JavaScript represents numbers using the 64-bit floating-point format defined by the IEEE 754 standard.

NOTE

8

JS

Page 9: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value.

The null keyword specifies that a variable does not hold any value. (the null value is not equal to zero because zero is a calculate value while null refers to the absence of a value)

TRIVIAL DATA TYPES

9

Page 10: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• Like many other programming languages, JavaScript has variables.• Variables can be thought of as named containers. • You can place data into these containers and then

refer to the data simply by naming the container.• Before you use a variable in a JavaScript program,

you must declare it. • Variables are declared with the var keyword.

10

JavaScript Variables

Page 11: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

DEFINING VARIABLES

11

<script type="text/javascript"> <!– var money; var name; //--> </script>

<script type="text/javascript"> <!– var money, name; //--> </script>

Page 12: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• Java Script is a case-sensitive language.• These rules are that a variable name:• Can consist of digit, underscore, and alphabets.• Must begin with a letter or underscore character• Cannot begin with a number and cannot contain

any punctuation marks.• Cannot contain any kind of special characters

such as +,*,%,and so on.• Cannot contain spaces• Cannot be a Java Script keyword

RULES TO DEFINED VARIABLES

12

Page 13: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• Storing a value in a variable is called variable initialization.

• You can do variable initialization at the time of variable creation or later point in time when you need that variable.

• For instance, you might create a variable named money and assign the value 2000.50 to it later. For another variable you can assign a value the time of initialization as follows:

<script type="text/javascript"> <!– var name = “Saif Ullah"; var money; money = 2000.50; //--> </script>

VARIABLE INITIALIZATION

13

Page 14: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

•  Use the var keyword only for declaration or initialization. Once for the life of any variable name in a document. You should not re-declare same variable twice.• JavaScript is untyped language. This means that a

JavaScript variable can hold a value of any data type. Unlike many other languages, you don't have to tell JavaScript during variable declaration what type of value the variable will hold. The value type of a variable can change during the execution of a program and JavaScript takes care of it automatically.

JAVA SCRIPT IMPORTANT HINTS

14

Page 15: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• The following are reserved words in JavaScript.

• They cannot be used as JavaScript variables, functions, methods, loop labels, or any object names.

JAVA SCRIPT KEYWORDS

15

abstractbooleanbreakbytecasecatchcharclassconstcontinuedebuggerdefaultdeletedodouble

elseenumexportextendsfalsefinalfinallyfloatforfunctiongotoifimplementsimportin

instanceofintinterfacelongnativenewnullpackageprivateprotectedpublicreturnshortstaticsuper

switchsynchronizedthisthrowthrowstransienttruetrytypeofvarvoidvolatilewhilewith

Page 16: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

ESCAPE SEQUENCES CHARACTERS

16

There are multiple escape sequence characters in JavaScript that provides various kind of formatting.

Page 17: Java script session 3

SCOPE OF VARIABLES

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

a) Global Variables: A global variable has global scope which means it is defined everywhere in your JavaScript code.

b) Local Variables: A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable.

Page 18: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• JavaScript has dynamic types. • This means that the same variable

can be used as different types:

EXAMPLE OF DYNAMIC VARIABLES

18

Examplevar x;               // Now x is undefinedvar x = 5;           // Now x is a Numbervar x = “Saif Ullah Dar";      // Now x is a String

Page 19: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• A string is a variable which stores a series of characters like “Saif Ullah Dar".• A string can be any text inside quotes. You

can use single or double quotes:

JAVA SCRIPT STRINGS

19

Examplevar carname="Volvo XC60";var carname='Volvo XC60';

You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

Examplevar answer="It's alright";var answer="He is called ‘Saif'";var answer='He is called “Mr Dar"';

Page 20: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• JavaScript has only one type of numbers. • Numbers can be written with, or without decimals:

JAVASCRIPT NUMBERS

20

Examplevar x1=34.00;      // Written with decimalsvar x2=34;         // Written without decimals Extra large or extra small numbers can be written with scientific (exponential) notation:

Examplevar y=123e5;      // 12300000var z=123e-5;     // 0.00123

Page 21: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

• Booleans can only have two values: true or false.• Booleans are often used in conditional testing.

You will learn more about conditional testing in a later chapter of this tutorial.

JAVA SCRIPT BOOLEANS

21

var x=true;var y=false;

Page 22: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar 22

<!DOCTYPE html><html><body>

<script>var firstname;firstname=“Saif";document.write(firstname);document.write("<br>");firstname=“Ullah";document.write(firstname);</script>

<p>The script above declares a variable,assigns a value to it, displays the value, changes the value,and displays the value again.</p>

<body></html>

Page 23: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

QUIZ:1

23

Inside which HTML element do we put the JavaScript?

(1)<javascript>

(2)<js>

(3)<script>

(4)<scripting>

Page 24: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

ANS:1

24

Inside which HTML element do we put the JavaScript?

(1)<javascript>

(2)<js>

(3)<script>

(4)<scripting>

Page 25: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

QUIZ:2

25

What is the correct JavaScript syntax to write "Hello World"?

(1)document.write("Hello World");

(2)echo "Hello World";

(3)("Hello World");

(4)response.write("Hello World");

Page 26: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

ANS:2

26

What is the correct JavaScript syntax to write "Hello World"?

(1)document.write("Hello World");

(2)echo "Hello World";

(3)("Hello World");

(4)response.write("Hello World");

Page 27: Java script session 3

04/11/2023 Developed By: Saif Ullah Dar

27S A I F U L L A H DA R

THANK YOU