A Data type is an attribute of data which tells the compiler how the programmer intend to use the data JavaScript unlike other programming languages has Seven data types. namely {Number , string, Boolean ,null, undefined ,object and Symbol }. This data types can also be classified into two kinds namely Primitive and non Primitive.
Primitive Data Types
A Primitive Data type is data that is not an object meaning cannot be broken down into smaller part. That is it contains the actual value directly in memory. There are Six (6) primitive Data types in JavaScript {Number, string, Boolean, null, undefined, symbol}
Number
JavaScript does not define different types of numbers like other programming languages The number type is used to represent both integer and floating-point numbers
String
Everything enclosed by quotes marks as a string in JavaScript. There are 3 types of quotes.
- Double quotes:
“ Hello Labs” - Single quotes:
‘ Hello Narabal’. -
Back-ticks:
HelloDouble and single quotes are termed as simple quotes, there is no difference between them in javascript however back-ticks extend the functionality of quotes which allow us to interpolate variables and expressions into a string by wrapping them in ${…}. the expression inside the ${…} is evaluated and the result becomes part of the string. let check the strings with the
typeofmethod
typeof ‘ labs’
typeof “hello”
typeof `name`Boolean
A Boolean type has two values it can only be a True or false this is mostly used to store yes or no values or a one or a zero value true means yes false means no
typeof true; //will output BooleanUndefined
Undefined is the absence of value. if a variable is declared but not assigned, then its value is set to undefine.
var noValue;
typeof noValue; // will output undefinedNull
null is a special value which means nonexistence of any value. null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
let name = null;The code above denotes that name is unknown
typeof null; // will output object objectthe null value is technically a primitive type typeof null is object this is due to the peculiarity with the way JavaScript was first defined. You can take it as a bug.
Symbol
ES6 comes with a lot of features and Symbol is one of them. Symbol is a unique value meaning two symbols will never equal the same value even if they have the same description.
var sym1 = Symbol(‘hello’)
var sym2 = Symbol(‘hello’)
sym1 == sym2 // return falseMeanwhile
var name = ‘hello’
var name2 = ‘hello’
name == name2 // return trueSymbol is used to create unique immutable values
Non-Primitive Data Type
They are types that are not defined by the programming language but are created by the programmer. This means properties can reference any type of data.
typeof {}; // will output object
typeof []; // will output object
typeof function a() {}; // will output functionfunctions and arrays are objects underneath.
