You are on page 1of 16

WEB TECHNOLOGY 512 CLASS

WEB TECHNOLOGY 512


Javascript Variables

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 2


Javascript Variables
What is Variable?

Variables are used to store data, like string of text, numbers, etc. The data or value stored in the
variables can be set, updated, and retrieved whenever needed. In general, variables are symbolic
names for values.
You can create a variable with the var keyword, whereas the assignment operator (=) is used to assign
value to a variable, like this: var varName = value;

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 4


Naming Conventions for JavaScript Variables

• A variable name must start with a letter, underscore (_), or dollar sign ($).
• A variable name cannot start with a number.
• A variable name can only contain alpha-numeric characters (A-z, 0-9) and
underscores.
• A variable name cannot contain spaces.
• A variable name cannot be a JavaScript keyword or a JavaScript reserved
word.

Note: Variable names in JavaScript are case sensitive, it means $myvar and
$myVar are two different variables.

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 5


abstract else instanceof switch
boolean enum int synchronized
break export interface this
byte extends long throw
case false native throws
catch final new transient
char finally null true
class float package try
const for private typeof
continue function protected var
debugger goto public void
default if return volatile
delete implements short while
do import static with
double in super

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 6


Example

<script>
// Creating variables
var name = "Samuel Peterson";
var age = 18;
var isStudent = true;

// Printing variable values


document.write(name + "<br>");
document.write(age + "<br>");
document.write(isStudent );

</script>

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 7


Lets add one more variable, we shall name it qualification

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 8


Example

<script>
// Creating variables
var name = "Samuel Peterson";
var age = 18;
var isStudent = true;
var qualification = “BSC IT Year 1”;
// Printing variable values
document.write(name + "<br>");
document.write(age + "<br>");
document.write(isStudent + <“br”>);
document.write(qualification);
</script>

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 9


Javascript Data Types
Data Types in JavaScript

Data types basically specify what kind of data can be stored and manipulated within a program.
There are eight basic data types in JavaScript which can be divided into three main categories:
primitive (or primary), composite (or reference), and special data types. String, Number, and
Boolean are primitive data types. Object, Array, and Function (which are all types of objects) are
composite data types. Whereas Undefined and Null are special data types.

Primitive data types can hold only one value at a time, whereas composite data types can hold
collections of values and more complex entities. Let's discuss each one of them in detail.

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 11


JavaScript primitive data types

There are five types of primitive data types in JavaScript. They are as follows:

Data Type Description

String represents sequence of characters e.g. "hello"

Number represents numeric values e.g. 100

Boolean represents boolean value either false or true

Undefined represents undefined value

Null represents null i.e. no value at all

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 12


JavaScript non-primitive data types

The non-primitive data types are as follows:

Data Type Description


Object represents instance through which we can access
members
Array represents group of similar values
RegExp represents regular expression

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 13


In a nutshell

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 14


Example

<script>
var courseName = "Web Technology 512";
var isLoggedIn = true;
var loggedcount = 75;

document.write("<B>Course Name is: " + courseName + "<br>");


document.write("<B>The Current logon status is: " + isLoggedIn + "<br>");
document.write("<B>The log count is: " + loggedcount);

</script>

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 15


THANK
YOU

[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 16

You might also like