4 Datatype
4 Datatype
Agenda
• Declaring Variables
• Data Types
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 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
• Null
• Undefined
• Boolean : Boolean represents a logical entity and can have two values: true, and false.
Example:
var x=true;
var y=false;
Number Data Type
Example:
var salary=40000;
var pi=3.14;
var phoneBill=2000;
String Data Type
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.
Example:
In simple words we can say that , null represents intentional absence of the
value
Example:
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 ?
a b
var msg=“ \”Hello\” “; var msg=‘ “Hello” ‘;
Displaying Variable Value
var a=10;
document.write(a);
var a=10;
document.write(‘Value of a is ‘+a);
Points To Remember
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.