0% found this document useful (0 votes)
18 views

03 1 of 2 JavaScript - Variables & Data Types

Uploaded by

bluegr3y3
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

03 1 of 2 JavaScript - Variables & Data Types

Uploaded by

bluegr3y3
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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;
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.
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
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>
Lets add one more variable, we shall name it isStudent
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>
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.
In a nutshell
JavaScript primitive data types

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


JavaScript non-primitive data types

The non-primitive data types are as follows:

Data Description
Type
Object represents instance through which we can
access members
Array represents group of similar values
RegExp represents regular expression
Example
<script>

var courseName = "Web Technology 2B";

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>

You might also like