Skip to main content

Command Palette

Search for a command to run...

Values and Types in JavaScript

Fundamentals of Values and Types in JavaScript

Published
3 min read
Values and Types in JavaScript

JavaScript is untyped/loosely/dynamically typed scripting language.

It means a variable in JavaScript is :

  • Not statically typed : The type of data (i.e. Number/String/Boolean) a variable is need not to be defined in advance.

  • Not Strongly typed : A JavaScript variable can hold any type of value over period of time

For Example

var a = 5;
a="hello"

What is a variable ?

A variable is a container, that holds reusable data to be used throughout the program

Variables are the named storage, that are used to represent the actual machine level storage address where actual data resides.

Tips for naming variables in JavaScript :

  1. JavaScript variable names must be written in Camel Case only

  2. JavaScript variable names must be as descriptive as possible, this helps in bug fixing and maintenance in future

e.g.

const myAwesomeBlogTitle = "How to write Nice Title";

Data Types in JavaScript

There are x types of data types in JavaScript :

1. Numbers

const numberVariable = 5; // The return value for type of both the Integer and the float is "number"
typeof numberVariable; // "number"

2. String

const greetString = "Hello";
typeof greetString; // "string"

3. Boolean

const booleanVariable = false;
typeof booleanVariable; // "boolean"

4. Null

const z = null;
typeof z; // "object"  Surprising ? It's a bug

5. undefined

const y  = undefined;
typeof y; // "undefined"

6. Symbol (Added in ES6)

const mySymbol = Symbol('literal');
typeof mySymbol; // "symbol"

7. Object

const myObject = {"name":"Steve", "age":5};
typeof myObject; // "object"

Note : The type of return value of "typeof" is always a "string" e.g.

const numberVariable = 5; //  
typeof numberVariable; // "number"
typeof typeof numberVariable; // "string" ; Because is it is returning type of "number" which is a string

In a broader sense, there are two types of variables in JavaScript

1. Primitive variables ( accessed by "Value") : The primitive variables are the ones which directly hold the value. e.g. Number, String, undefined, null, Symbol etc

var a = 5;
var b = a ;
console.log(a);  // 5  ; value 5 is assigned to a
console.log(b); // 5   ; value stored in a i.e. 5 is assigned to b
a = 10; 
console.log(a); // 10  ; new value 10 is assigned to a
console.log(b); // 5   ; b is holding the value as assigned earlier i.e. 5

2. Compound variables ( accessed by "Reference") : Objects are the compound variable in JavaScript

A) Object : An Object has one or more properties assigned to it, each property h has name/value pair.

e.g.

var myObject = {"name":"John" , "age":"32"}; //
typeof myObject; // The typeof myObject is "object" & it has two properties having key "name"/"age" & values as "John"/32 respectively

B) Array : Array is also subtype of object but it has same type of properties.

var myArray = [1,2,3,4];
typeof myArray // "object"
myArray.length // 4
myArray[0] // 1 i.e. value at 0th index 
myArray[1] // 2 i.e. value at 1st index

C) Function : Functions are also object subtypes but typeof returns "function", which implies that a function is a main type -- and can thus have properties

function myFunc() { 
return 5; 
}

typeof myFunc; // "function"
typeof myFunc(); // "number" ; since it is evaluating the return value of function rather than function
myFunc.customProp = "hello" ; 
myFunc.customProp; // "hello" ; this proves that the functions are object subtypes

This blog is part of my series JavaScript Made Easy, where I write about my learning experience with JavaScript and best practices for design and developing solutions using it.

Subscribe to get the regular updates.

D

Well done, you covered all types that so many forget, but beware, try not to use var any longer, (unless you have cut and paste the example from somewhere, in which case you need to cite it) 😉

1

JavaScript Made Easy

Part 2 of 4

Series of posts on my learning experience with JavaScript, Will discuss about common misconceptions and truths about JavaScript The best practices in JavaScript development will be shared

Up next

Coercion & Comparison in JavaScript 🛠

Understating Basics of Coercion, Comparison, Equality in JavaScript