0% found this document useful (0 votes)
8 views8 pages

JavaScript Data Types Explained

gvbg

Uploaded by

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

JavaScript Data Types Explained

gvbg

Uploaded by

Prema Ranganath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JavaScript / ECMAScript

JavaScript was invented by Brendan Eich in 1995.

It was developed for Netscape 2, and became the ECMA-262 standard in


1997.

After Netscape handed JavaScript over to ECMA, the Mozilla foundation


continued to develop JavaScript for the Firefox browser. Mozilla's latest
version was 1.8.5. (Identical to ES5).

Internet Explorer (IE4) was the first browser to support ECMA-262 Edition 1
(ES1).

JavaScript is a lightweight and dynamic computer programming language.


It is used to create client-side dynamic pages.
It is an open-source and cross-platform language.
Basic Syntax:
[Link]("Basic Print method in JavaScript");

What are Primitive and Non-Primitive Data Types in JavaScript?

Data Types − Every variable in JavaScript has a data type that defines the type of variable it
is and what kind of data is being stored in the variable. There are two types of data types
that are available in JavaScript namely
Primitive Data Types and Non-Primitive Data Types.

Primitive Data Types − This is the predefined data type that is provided by
JavaScript for different usages. These are also known as the in-built data types.
Non-Primitive Data Types − These data types are derived from the primitive data
types and work as a reference. Therefore, they are also known as reference data
types.

Primitive Datatypes
Different types of Primitive Datatype are −
[Link] −This data type can hold the decimal values as well as the without
decimal values in JavaScript.

Example 1
In the below example, we will be exploring the Number data type.

<html>
<head>
<title>Data Types</title>
</head>
<body>
<h1 style="color: red;">
Javascript

JavaScript Program to Add Two Numbers Using Form


Output:

2. String Datatype − The String datatype in JavaScript is used for representing a sequence
of characters that is surrounded by single or double quotes.

<html>
<head>
<title>Data Types</title>
</head>
<body>
<h1 style="color: red;">
Welcome To Javascript
</h1>
<script>
let str = 'Hello User...';
let str1 = "Welcome to String Datatype";
[Link]("String: " + str);
[Link]("String 1: " + str1);
</script>
</body>
</html>
Output:
It will produce the following output in the Console.
String: Hello User...
String 1: Welcome to String Datatype

3. Undefined − The undefined data type is used when the value of any request or response
cannot be defined by JavaScript. For example: Initializing a number without value.

<html>
<head>
<title>Data Types</title>
</head>
<body>
<h1 style="color: red;">
Welcome To Undefined Datatype
</h1>
<script>
let x;
[Link]("Value of x: " + x);
x = undefined
[Link]("x: " + x);
</script>
Output
</body>
</html
It will produce the following output in the Console.

It will produce the following output in the Console.


String: Hello User...
String 1: Welcome to Undefined Datatype

4. Boolean − The boolean data type only accepts two types of values i.e. either true
or false.
5. Null − This only accepts null values.

<html>
<head>
<title>Data Types</title>
</head>
<body>
<h1 style="color: red;">
Welcome To Boolean and Null Datatype
</h1>
<script>
let x = null;
let bool = true;
Output
It will produce the following output in the Console.
Boolean value: true
Value of x:null
Dividing 0 by 0: NaN

Non-primitive datatypes
The non-primitive data types are as follows −
1. Object − The objects in JavaScript are entities that have properties and methods.
Everything in Java is an Object.
Creating an Object in JavaScript
Defining an Object using constructor:

// Create an empty generic


object
var obj = new Object();

//Create a user defined object


var mycar = new Car();

Defining an Object using literal annotations –

// An empty object
var square = {};

// Here a and b are keys and


// 20 and 30 are values
var circle = {a: 20, b: 30};
Example:
<html>
<head>
<title>Data Types</title>
</head>
<body>
<h1 style="color: red;">
Welcome To Tutorials Point
</h1>
<script>
// Creating object with the name person
let student = {
firstName: "Hello",
lastName: "World",
};
// Printing the value on console
[Link]("Full Name: " + [Link] + " " +
[Link]);
</script>
</body>
</html>
Output
The output is displayed in the Console.
Full Name: Hello World

2. Array − Arrays in JavaScript can be used for storing more than one element
under a single name.

Declaring a 1D array −
// Call it with no arguments
var a = new Array();

// Call it with single numeric argument

var b = new Array(10);


// Explicitly specify two or
// more array elements
var d = new Array(1, 2, 3, "Hello");

<html>
<head>
<title>Data Types</title>
</head>
<body>
<h1 style="color: red;">
Welcome To Array datatype
</h1>
Let us see the actual JavaScript program that lets the user give the input using Form and
<script>
Textbox.
var x = new Array();
var y = new Array(21);
<!doctype
var zhtml>
= new Array("Hello", 21, "Tutorials", "Point", 32, 56);
[Link]("value of x: " + x);
[Link]("value of y: " + y);
[Link]("value of z: " + z);
</script>
</body>
</html>

Output
The above program will produce the following output in the Console.
value of x:
value of y: ,,,,,,,,,,,,,,,,,,,,
value of z: Hello,21,Tutorials,Point,32,56

<!doctype html>

<html>

<head>

<title>Adding two numbers using JavaScript in HTML</title>

<script>

var num1 = 10;

var num2 = 40;

var sum = num1+num2;


[Link]("Sum of two numbers is " + sum);

</script>

</head>

<body></body>

</html>

JavaScript Program to Add Two Numbers Using Form


and TextBox
Let us see the actual JavaScript program that lets the user give the input using Form and
Textbox.

<html>

<head>

<script>

function add()

var num1, num2, sum;

num1 = parseInt([Link]("firstnumber").value);

num2 = parseInt([Link]("secondnumber").value);

sum = num1 + num2;

[Link]("answer").value = sum;

</script>

</head>

<body>

<p>First Number: <input id="firstnumber"></p>

<p>Second Number: <input id="secondnumber"></p>

<button onclick="add()">Add Them</button>

<p>Sum = <input id="answer"></p>

</body>

</html>

Output:

You might also like