0% found this document useful (0 votes)
16 views23 pages

4 Datatype

Uploaded by

yevitoy306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views23 pages

4 Datatype

Uploaded by

yevitoy306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

VARIABLES AND DATA TYPES

Agenda

• Declaring Variables

• Data Types

• Printing Double Quotes


Declaring Variables
JavaScript is a dynamically typed programming language.

Unlike C++ or Java , in JavaScript we do not have to declare a


variable’s type when we define it.

Rather JavaScript itself decides the data type of the variable


depending on the value we assign to that variable .
Declaring Variables
However JavaScript provides us 4 ways to declare and initialize a
variable.

These are var, let, const and 4th way is directly assigning the value

We will start with var and let and then proceed to other ways

Syntax: Syntax:

var <var name>; let <var name>;


OR OR
var <var name>=<value>; let <var name>=<value>;
Declaring Variables
 var firstName;  var firstName, lastName;
OR OR
let firstName; let firstName,lastName;
declaring one javascript variable declaring multiple
javascript variables


 var firstName = ‘Sachin’, lastName = ‘Kapoor’;
var firstName = ‘Sachin’;
OR
OR
let firstName = ‘Sachin’, lastName = ‘Kapoor’;
let firstName=‘Sachin’;
declaring and assigning one declaring and assigning
javascript variable multiple javascript variables
Variable Declaration Rules

Variable names must begin with a letter and can have digits also

Variable names can also begin with $ and _

Variable names are case sensitive

Default value of a variable is “undefined”


Data Types
There are two types of data types:

Primitive data types Non Primitive data types


• Boolean
• Object
• Number
• Array
• String

• Null

• Undefined

• Symbol (Added by ES6 )

• BigInt (Added by ES2020)


Primitive data types

• Boolean : Boolean represents a logical entity and can have two values: true, and false.

• Number : Represents numeric values e.g. 100

• String : Represents sequence of characters e.g. "hello"

• Null : Represents null i.e. no value at all

• Undefined : Represents undefined value

• Symbol : Represents unique keys

• BigInt: Represents very large integer values


Non Primitive data types

• Object : Represents instance through which we can access members

• Array : Represents group of similar values


Boolean Data Type

Booleans can only have two values: true or false.

Booleans are often used in conditional testing.

Example:

var x=true;
var y=false;
Number Data Type

Numbers in JavaScript are just numbers , no integers and no floats , just


plain numbers

Example:

var salary=40000;
var pi=3.14;
var phoneBill=2000;
String Data Type

A String is just a Any type of It is enclosed in a


combination of character can be pair of “ “ or ‘
words including stored in a string. ‘.
nos.

Example: var myName=“Sachin”;


OR
var myName=‘Sachin’;
undefined And null

null and undefined are both data types in JavaScript.

Understanding the differences between them and when to use what is very
important for a beginner in the JavaScript language
What Is undefined ?

It means a variable has been declared, but no value has been assigned to it.

In simple words we can say that , JavaScript automatically assigns undefined


to any variable which is declared but not initialized

Example:

var age ; // age is declared but not initialized


console.log(age); // Will show undefined
Other Places Where undefined Is Used?

Accessing an array index or an object property that does not exist.

Calling a function, without it’s required function parameters.

A function not returning anything , returns undefined.


What Is null ?

It is a value that is assigned by programmer, to indicate that the variable


currently has no value but will be initialized later

In simple words we can say that , null represents intentional absence of the
value

Example:

var age=null ; // age is declared and initialized with null


console.log(age); // Will show null
The Difference

null undefined
It is an assignment value. It can be assigned to a variable It is not an assignment value. It means a variable has been
which indicates that a variable does not point to any object. declared but has not yet been assigned a value.
It is an object. It is a type itself.

null indicates the absence of a value for a variable. undefined indicates the absence of the variable itself.

null is converted to zero (0) while performing primitive undefined is converted to NaN while performing primitive
operations. operations.
Printing Double Quotes

?
How would you print “Hello” within double quotes ?

To print double quoted string we have 2 ways

a b
var msg=“ \”Hello\” “; var msg=‘ “Hello” ‘;
Displaying Variable Value

To display a variable’s value we just have to pass it’s name to the


document.write( ) method.

var a=10;
document.write(a);

To print a variable’s value with some text we use operator “ + “

var a=10;
document.write(‘Value of a is ‘+a);
Points To Remember

JavaScript Has Dynamic Types

This means that the same variable can be used as different types:

Example:

var x ; // x is undefined
x = 5; // Now x is a Number
x = "John"; // Now x is a String
Enhancements Made By ES6

ES6 has given 2 new keywords for declaring variables and they are called let
and const .
The keyword let , just like var allows us to declare variables whose value can
change , while the keyword const creates variables whose values cannot
change.

Also , it is
Example: compulsory to
initialize a const
variable at
let x =5 ; // x is 5 the time of
declaration , otherwise
x = 10; // Now x is 10 JS will give error
const pi=3.14; //pi is a constant
pi = 4.14; // Error!
var V/s let

There are many differences between var and let and we will discuss them as
the course progresses.

As of now we must remember that var was supported from original JS while
let was introduced by Modern JS.

You might also like