JavaScript Data Types
Data can come in many different types. The data types depend on the values which are hold by the variable. The following lesson covers the JavaScript dynamic data types , primitive data types of string, boolean, and number, as well as the built-in functions for modifying values of these types.Dynamic Typing
JavaScript is a loosely typed or a dynamic language. With a loosely typed language, you don't have to declare ahead of time that a variable will be a string or a number or a boolean, as the data type is actually determined while the application is being processed. Unlike many other languages, in JavaScript , the same variable can hold different types of data, all within the same application. It acts as dynamically means same variable can be used as different types, regardless of whether the data is a string, number, boolean, array, or other object—so that you can access the same data again and again. A string variable holds a string ; a number variable holds a number value, and so on. JavaScript Variables are declared using var statement . The var keyword inform the computer that it needs to reserve some memory for your data to be stored in later. You can declare multiple variables at once. If you not initialize your variable in var statement, It's automatically assume values is undefined . To declare a variable as var type , use the var operator followed by the variable name.
var anyType;
example
var anyType = 101; // anyType is now a Number
var anyType = 'John'; // anyType is now a String
var anyType = true; // anyType is now a Boolean
You can assign Var variables with the Value of Other Variables.
var oldVar;
var newVar;
oldVar = 3.14;
newVar = oldVar;
You can combining 'Var' Data Types
var total = 100 + 50 + 'is the total';
output is 150 is the total
Even though Javascript is loosely typed language , it actually does have data types (also called primitive types). Normally there are three Primary data types and two Composite data types and Two special Data Types in Javascript.Primary Data Types (Primitive)
- String
- Number
- Boolean
Composite Data Types (reference)
- Object
- Array
Special Data Types
- Null
- Undefined
JavaScript String
A string literal is a sequence of characters delimited by single or double quotes. It exist within either single quotes ' or double quotes ", so to create a string , enclose a sequence of characters in quotes.
var str = 'Javascript string.;
var str = "Javascript string";
More about... JavaScript Strings JavaScript Number
A number by itself is a number primitive , all numbers in JavaScript are 64-bit floating-point numbers. Numbers can be written with or without decimals. They represent numeric values. The simplest type of number is an integer with no fractional component. Another type is floating point number which is assumed to have decimal point.
var intNum = 100; // Numeric integer value
var floatNum = 3.14; // Numeric float value
var num = -25.12; // Negative numeric float value
JavaScript Boolean
JavaScript Boolean type can have two value true or false. Boolean type is use to perform logically operator to determine condition/expression is true.
var isTrue = true;
var isTrue = false;
JavaScript Object
JavaScript Object is a collections of properties and methods. In Javascript, anything that is not a primitive is an Object. Objects in JavaScript can be seen as a collection of properties. Property values can be values of any type, including other objects, which enables building complex data structures . Properties are identified using key values. A key value is either a String or a Symbol value.
var student = {Name:"John", Class:"12", Grade:"C"};
JavaScript Array
JavaScript Arrays are regular objects, both objects and arrays can have properties and methods. Array values must be belonging opening or closing bracket.
var language = ["J", "a", "v", "a", "S", "c", "r", "i", "p", "t"];
More about... JavaScript Array JavaScript Null
Null is empty value . The null keyword cannot be used as name of the variable. In JavaScript null is not same as 0. It is absence of any value. If we try to reference a value for variable which is not defined and has no value then it returns a null value .
var nullTest=null;
alert(nullTest); //shows null value
JavaScript Undefined
A variable that has not been assigned a value has the value undefined .JavaScript Symbol Data types
JavaScript Symbol data type new (Currently ECMAScript 6 Drafted) used for identifier unique object properties.
var symb1 = Symbol();
var symb1 = Symbol('anything');
Related Topics