Data Type Of JavaScript Day 3

Lecture 3 : Data Types Of JavaScript

Data Types :
  • Data type variables can hold many data types: string, number, boolean.
  • To be able to operate on variables, it is important to know something about the type.
  • There are three main data types in JavaScript:
          1) string
          2) number
          3) boolean

1) string :
  • A string is a series of characters like "John Doe".
  • String are written with quotes. You can use single or double quotes:
Example :

      1) var name = "Mayuri"; // using double quotes
      2) var name = 'Mahesh'; // using single quotes

2) number :
  • JavaScript has only one type of numbers.
  • Numbers can be written with, or without decimals.
Example :

      1) var x1 = 34.00 ; // using with decimal
      2) var x2 = 34; // using without decimal

3) boolean :
  • Booleans can only have two values: true or false.
  • Booleans are often used in conditional testing.
Example :

      var x = 5;
      var y = 5;
      var z = 6;
      (x == y) return true
      (x == z) return false

Example :

Program-

<html>
<body>
<script>
var x=0;
alert(typeof(x));
x = "Mayuri";
alert(typeof(x));
x = true;
alert(typeof(x));
</script>
</body>
</html>

Output-




The typeof Operator :
  • You can use the JavaScript typeof operator to find the type of a JavaScript variables.
  • The typeof operators returns the type of a variable or an expression.
Example :

      var x=0;

      alert(typeof(x));
      x = "Mayuri"; // using string

      alert(typeof(x));
      x = true; // using boolean

      alert(typeof(x)); //number

Prototypical Language :
  • All JavaScript objects inherit properties and methods from a prototype.
  • A prototype based language has the notion of a prototypical object, an object used as a template from which to get the initial properties for a new object.
  • There are two ways of creating objects in prototypical language:
              1) Literal way of creating object
              2) Constructor way of creating object


1) Literal way of creating object :
  • Literals are smaller and simpler ways to define objects.
  • We simple define the property and values inside curly braces.
  • Using an object literal, you both define and create an object in one statement.

Example :

Program-

<html>
<body>
<script>
var student = { };
student.name = "Mayuri";
student.age = function()
{
alert(21);
alert(student.name);
}
student.age();
</script>
</body>
</html>

Output-


2) Constructor way of creating object :
  • One of the easiest way to instantiate an object in JavaScript. 
  • Constructor is nothing but a function and with help of new keyword, constructor function allows to create multiple objects.

Example :

Program-

<html>
<body>
<script>
function Person()
{
this.name = "writing blog";
this.javascript = function()
{
alert("javascript");
alert(this.name);
}
}
var obj = new Person();
Obj.javascript();
</script>
</body>
</html>

Output-

 

Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

Understanding C Sharp Day 18

Learn Garbage Collection Day 21