Javascript Data Types

JavaScript Data Types:-

     JavaScript variables:-

  1.  String

  2. Number

  3. Boolean

  4. Array

  5. object

1. String:-

A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. Strings can be created by enclosing the string literal (i.e. string characters) either within single quotes (') or double quotes ("), as shown in the example below:

<!DOC TYPE HTML>
<HTML Lang=”en”>
<head>
<meta char set=”utf-8″>
<title>Creating Strings in JavaScript</title>
</head>
<body>
<script>
// Creating variables
var my String = ‘Hello World!’; // Single quoted string
var my String = “Hello World!”; // Double quoted string

// Printing variable values
document.write(my String + “<br>”);
document.write(my String);
</script>
</body>
</HTML>

Result:-Hello World!
Hello World!

2. Number:-

The Number is a primitive data type in JavaScript. Number type represents integer, float, hexadecimal, octal or exponential value. First character in a Number type must be an integer value and it must not be enclosed in quotation marks.

Example: Number in JavaScript

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Numbers</h2>

<p>Numbers can be written with, or without decimals:</p>

<p id=”demo”></p>

<script>
var x1 = 34.00;
var x2 = 34;
var x3 = 3.14;

document.getElementById(“demo”).innerHTML =
x1 + “<br>” + x2 + “<br>” + x3;
</script>

</body>
</html>

Result:-JavaScript Numbers

Numbers can be written with, or without decimals:

34
34
3.14

3. Boolean:-

Boolean can only have two values: true or false.

Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Booleans</h2><p>Booleans can have two values: true or false:</p><p id=”demo”></p><script>
var x = 5;
var y = 5;
var z = 6;
document.getElementById(“demo”).innerHTML =
(x == y) + “<br>” + (x == z);
</script>

</body>
</html>
Result:-

JavaScript BooleansBooleans can have two values: true or false:

true
false

4. Array:-

JavaScript arrays are written with square brackets.

Array items are separated by commas.

The following code declares (creates) an array called cars, containing three items (car names):

Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2><p>Array indexes are zero-based, which means the first item is [0].</p><p id=”demo”></p><script>
var cars = [“Saab”,”Volvo”,”BMW”];

document.getElementById(“demo”).innerHTML = cars[0];
</script>

</body>
</html>

Result:-JavaScript ArraysArray indexes are zero-based, which means the first item is [0].

Saab

5. object:-

JavaScript objects are written with curly braces {}.

Object properties are written as name:value pairs, separated by commas.

Example

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Objects</h2>

<p id=”demo”></p>

<script>
var person = {
firstName : “John”,
lastName  : “Doe”,
age     : 50,
eyeColor  : “blue”
};

document.getElementById(“demo”).innerHTML =
person.firstName + ” is ” + person.age + ” years old.”;
</script>

</body>
</html

Result:-JavaScript Objects

John is 50 years old.