Javascript Object Oriented Design - Javascript Primitive Methods








Javascript primitive types, strings, numbers, and Booleans actually have methods.

The null and undefined types have no methods.

String

Strings have many methods to work on string value.

For example:

 
var name = "Javascript is easy"; 
var lowercaseName = name.toLowerCase();     
var firstLetter = name.charAt(0);           
var middleOfName = name.substring(4, 10);   

console.log(lowercaseName);
console.log(firstLetter);
console.log(middleOfName);

The code above generates the following result.





Number

The following code calls methods from number primitive types.

       
var count = 10; 
var fixedCount = count.toFixed(2);          
var hexCount = count.toString(16);   

console.log(fixedCount);
console.log(hexCount);

The code above generates the following result.

Boolean

The following code calls toString method on boolean values


var flag = true; 
var stringFlag = flag.toString();           

console.log(stringFlag);

The code above generates the following result.





Note

Even if they have methods, primitive values are not objects.