Javascript Data Types
By: Hassan Raza
Tags: Arrays, Js, Data Type, Values

History & Introduction:

From the earliest days of the Web, JavaScript has been a foundational technology that drives interactive experience around the content we consume. While flickering mouse trails and annoying popup prompts may be where JavaScript started, nearly two decades later, the technology and capability of JavaScript has grown many orders of magnitude, and few doubt its importance at the heart of the world’s most widely available software platform: the Web.

“JavaScript” is as related to “Java” as “Carnival” is to “Car.”

Because JavaScript borrows concepts and syntax idioms from several languages, including proud C-style procedural roots as well as subtle.

While JavaScript is perhaps one of the easiest languages to get up and running with.

Types:

A type is an intrinsic, builtin set of characteristics that uniquely identify the behavior of particular value and distinguishes it from other values, both to the engine and to the developer.

Built-in Types:

JavaScript defines seven built-in types:

• null

• undefined

• boolean

• number

• string

• object

• symbol — added in ES6!

All of these types except object are called “primitives.”

Primitives:

Anything that is not an object, It can be String, Number, Null, Undefined. Primitive value get passed by value. primitive gets it’s own sopt in memory

Non Primitive:

Non Primitive value would be an object, array, a function, or promise. Non Primitive value get passed by refrence. Non Primitive share same place in memory. If you change the non primitive value it can change things in unusal ways.

The “typeof” operator inspects the type of the given value, and always returns one of the seven string values.

Objects

Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life. The concept of objects in JavaScript can be understood with real life, tangible objects.

A JavaScript object has properties associated with it. A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects. The properties of an object define the characteristics of the object. You access the properties of an object with a simple dot-notation:

var myCar = new Object();

myCar.make = 'Honda';

myCar.model = 'Civic';

myCar.year = 2016;

Arrays

The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.

JavaScript arrays are just containers for any type of value, from string to number to object to even another array (which is how you get multidimensional arrays):

var a = [ 1, “2”, [3] ];

a.length; // 3

a[0] === 1; // true

a[2][0] === 3; // true

Arrays are numerically indexed (as you’d expect), but the tricky thing is that they also are objects that can have string keys/properties added to them.

Generally, it’s not a great idea to add string keys/properties to

arrays. Use objects for holding values in keys/properties, and save

arrays for strictly numerically indexed values.

Strings

The String object is used to represent and manipulate a sequence of characters. Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length.

It’s a very common belief that strings are essentially just arrays of characters. While the implementation under the covers may or may not use arrays, it’s important to realize that JavaScript strings are really not the same as arrays of characters. The similarity is mostly just skin-deep.

var a = "foo";

var b = ["f","o","o"]; a.length; // 3

b.length; // 3 a.indexOf( "o" ); // 1

b.indexOf( "o" ); // 1 var c = a.concat( "bar" ); // "foobar"

var d = b.concat( ["b","a","r"] ); // ["f","o","o","b","a","r"] a === c; // false

b === d; // false a; // "foo"

b; // ["f","o","o"] a.join; // undefined

a.map; // undefined a.reverse; // undefined

b.reverse(); // ["!","o","O","f"]

JavaScript strings are immutable, while arrays are quite mutable.

Numbers

JavaScript has just one numeric type: number. This type includes both “integer” values and fractional decimal numbers.

Number literals are expressed in JavaScript generally as base-10 decimal literals.

For example:

var a = 42; var b = 42.3;

var a = 0.42; var b = .42;

By default, most numbers will be outputted as base-10 decimals, with trailing fractional 0s removed. So:

var a = 42.300;

var b = 42.0; a; // 42.3

b; // 42

Undefined

Undefined means there’s definitely a variable and at the moment it has no value.

Variables that have no value currently actually have the undefined

value. An “undefined” variable is one that has been declared in the accessible scope, but at the moment has no other value in it.

It’s like the vacuum when there is no other value, undefined will be there.

The global undefined property represents the primitive value undefined. It is one of JavaScript’s primitive types. “Undefined” is a global variable that JavaScript creates at run time.

This global variable is assigned to an object in one of the following cases:

1. Declared but not defined –

JavaScript assigns ‘undefined’ to any object that has been declared but not initialized or defined. In other words, in a case where no value has been explicitly assigned to the variable, JavaScript calls it ‘undefined’.

2. Array index or object property that does not exist.

3. A function parameter that has not been supplied.

4. The return value of functions that have to but don’t return a value.

An illustration of this assignment is shown in the following –

var item; console.log(item)

In non-strict mode, it’s actually possible to assign a value to the globally provided undefined identifier:

function foo() {

undefined = 2; // really bad idea!

} foo(); function foo() {

"use strict";

undefined = 2; // TypeError!

}

Uninitialized

Uninitialized means a variable never even has any value & never declare, like out of scope variable.

Undeclared

Undeclared means it’s has not been created in any scope that we have access to.

Boolean

Boolean is a data type that returns either of two values i.e. true or false. In JavaScript, boolean is used as a function to get the value of a variable, object, conditions, expressions, etc. in terms of true or false.

var x = Boolean(expression); // use this...

var x = !!(expression); // ...or this

var x = new Boolean(expression); // don't use this!

Null

The value null represents the intentional absence of any object value. It is one of JavaScript’s primitive values and is treated as falsy for boolean operations. For the null type, there is one and only one value: null

Both undefined and null are often taken to be interchangeable as either “empty” values or “non” values.

Other developers prefer to distinguish between them with nuance. For example:

• null is an empty value.

• undefined is a missing value

OR

• undefined hasn’t had value yet.

• null had a value and doesn’t anymore.

typeof null === "object"

It’s a bug in JS, when do “typeof” with the null it returns object.

Then how you will check the value???

var a = null;

(!a && typeof a === "object") // true

Null is the only a primitive value that is falsy but which also return object from “typeof” check.

Summary

In JavaScript, variables don’t have types, values have types. Variables

can hold any value, at any time. JavaScript has seven built-in types: null, undefined, boolean, number, string, object, and symbol. They can be identified by the “typeof” operator.

In JavaScript, arrays are simply numerically indexed collections of any value type. strings are somewhat “array-like,” but they have distinct behaviors and care must be taken if you want to treat them as arrays. Numbers in JavaScript include both “integers” and floating-point values. The null type has just one value, null, and likewise, the undefined type has just the undefined value. undefined is basically the default value in any variable or property if no other value is present.

Generally, it’s not a great idea to add string keys/properties to

arrays. Use objects for holding values in keys/properties, and save

arrays for strictly numerically indexed values.