Day 3 : JavaScript
Data Types of JavaScript:
- In JavaScript, their are three data types present i.e number, string and boolean.
- As we all know JavaScript is a dynamic language, means one variable can hold different datatypes.
Example :
var x;
x=10; //number
x="Hello" //string
// In above eg. the x holds number as well as string but shows string.
- JavaScript evaluates expressions from left to right.
Example 1- var x=20+5+"Hi"
//o/p - 25Hi
Example 2- var y= "Hi"+23+1;
//o/p - Hi231
- In above eg. 1, the first operand is number so it treats whole expression as number i.e it adds 20 and 5 , after that it adds Hi in that.
- In above eg. 2, the first operand is string so it treats whole expression as string i.e it concat all strings like above output. It doesn't add 23 and 1, it concat both with Hi.
Typeof operator:
- We use typeof operator in JavaScript to find the datatype of the variable.
- The typeof operator returns the datatype of variable.
- Example of typeof operator and datatype :
- Output :
JavaScript is a Prototypical language :
- The objects in the JavaScript inherits properties and methods of prototype.
- Their are two ways of creating object in prototype -
1) Literal way of creating object :
eg. var person = {};
2) Constructor way of creating object :
eg. //template
function person() {
this.name=function(){
alert("Pooja");
}
}
//instance
var p=new person();
p.name();
- Program for both ways:
- Output :
//Literal way-
//Constructor way-
Thank you!!!
For more understanding watch below video :
JavaScript Day 1 : Day 1
JavaScript Day 2 Global Pollution : Day 2 Global
JavaScript Day 2 Undefined and Hoisting : Day 2 Undefined
Comments
Post a Comment