AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

JavaScript Types


JavaScript Tutorial » JavaScript Types

Data types basically specify what kind of data can be stored and manipulated within a program.

There are six basic data types in JavaScript which can be divided into three main categories:
1. primitive (or primary) as: String, Number, and Boolean
2. composite (or reference) as: Object, Array, and Function
3. special data types. Object as: Undefined and Null.

Primitive data types can hold only one value at a time, whereas composite data types can hold collections of values and more complex entities.

The String Data Type

String Data Type represent textual data. Strings are created using single or double quotes.

Example:
var x = 'Have a good day!'; // single quotes
var y = "Have a good day!"; // double quotes
var xa = "Let's have a good day"; // single quote inside double quotes
var xb = 'He said "Have a good day" and left.'; // double quotes inside single quotes
var xc = 'We\'ll have a good day.'; // escaping single quote with backslash


In JavaScript, variables can also be declared without having any initial values assigned to them. This is useful for variables which are supposed to hold values like user inputs.

Example: This example will show how JS statements look like:
// Declaring Variable
var userSurName;

// Assigning value
userSurName = "Langford";


Number Data Type - example


The number data type is used to represent positive or negative numbers with or without decimal.

Example:

<script>
var x = 74; // integer
var y = 74.5; // floating-point number
var z = 74.25e+7; // exponential notation, same as 74.25e7 or 742500000
var w = 74.25e-7; // exponential notation, same as 0.000007425

alert(12 / 0); // Output: Infinity
alert(-12 / 0); // Output: -Infinity
alert(12 / -0); // Output: -Infinity

alert("your text" / 2); // Output: NaN
alert("your text" / 2 + 10); // Output: NaN
alert(Math.sqrt(-1)); // Output: NaN
</script>



Boolean Data Type - example


The Boolean data type can hold only two values: true or false.

Used to store values like yes (true) or no (false), on (true) or off (false).

Example:
<script>
var isWorking = true; // yes, I'm working
var iswrite = false; // no, I'm not write

var x = 4, y = 10, z = 19;

alert(y > x) // Output: true
alert(y > z) // Output: false
</script>

 

Object Data Type

he object is a complex data type that allows you to store collections of data.

Example:
<script>
var emptyObject = {};
var person = {"name": "Salwyn", "surname": "Jara", "age": "27"};

// For better reading
var car = {
"modal": "Renault Megane",
"color": "red",
"doors": 3
}
</script>


Array Data Type, example

An array is a type of object used for storing multiple values in single variable.

The array index starts from 0, so that the first array element is arr[0] not arr[1].

Example: a is defined as a variable, and a is given the value 15:

<script>
var colors = ["green", "red", "yellow", "orange"];
var cities = ["Roma", "Valencia", "Madrid"];

alert(colors[0]); // Output: green
alert(cities[2]); // Output: Madrid
</script>





javascript types of variables, of alerts, list, of functions, of arrays, errors, loops, events, objects
JavaScript Types - javascript tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.