JavaScript Primitives Types

JavaScript

JavaScript has 6 primitive data types (a primitive type is data that is not an object) which lets the JavaScript engine differentiate between different categories of data and they are as follows.

  • string
  • number
  • boolean
  • undefined
  • null
  • symbol (new in ECMAScript 2015)

String


// Strings
"Hello World with double quotes"
'Hello World with single quotes'
"28" // see note

In JavaScript a string is always inside quotes (single or double). As with any good code convention, when it comes to quotes, define it once and stick with it. Don’t mix and match.

Note: Numbers wrapped in quotes are treated as strings.

Number

In JavaScript a “number” type is a floating point number (decimals). Unlike some other languages, JavaScript doesn’t care if a number is a whole, decimal or negative number.


// Numbers
2
5.6
-3

Boolean

Booleans are simply true or false which makes they extremely useful for conditional statements and logic.


// Booleans
true
false

undefined

In JavaScript a variable is automatically assigned in memory the value undefined. Undefined represents a lack of existence.


var a; // undefined
var b = "I have a value!";
Note: You should never set a variable to this value yourself, the JavaScript engine will do it for you.

null

null also represents a lack of existence, but this time you can set this value yourself when you want to set a variable equal to nothing.


var a = null; // null - equal to nothing 
var b = "I have a value!";

Symbol

According to MDN, “a symbol is a unique and immutable data type. It may be used as an identifier for object properties.”

This will be updated once I become more familiar with the symbol primitive as we move to ES6.

Further Reading:

https://developer.mozilla.org/en-US/docs/Glossary/Primitive

Matthew Horne

Matthew is a web developer from the United Kingdom who taught himself PHP and JavaScript and never looked back. If you would like to hire me, shoot me an email.