JavaScript Data Types

javascript data types

JavaScript variables can hold many data types: Number, BigInt, String, Boolean, Undefined, Symbols, Arrays, objects, etc

var number = 16; // Number
var color = “red”; // String
var isRed = true; // Boolean
var car = {color:”red”, type:”SUV”}; // Object

JavaScript Primitive Types

In JavaScript are 7 primitive data types. Number, BigInt, String, Boolean, Undefined, Null, Symbol are primitive types.

Primitive data types characteristics

  • Primitives are compared by value, they don’t have individual identities
  • A primitive is not an object and has no methods of its own.
  • All primitives are immutable, meaning they cannot be altered

JavaScript Numbers

JavaScript numbers can be with, or without decimals:
var x = 4;
var y = 4.42;

Extra-large or extra small numbers can be written with the exponential notation:
var y = 442e5; // 44200000
var z = 442e-5; // 0.00442

The JavaScript’s highest integer value that a number can go to without losing precision is +/- 9007199254740991

JavaScript BigInt

In JavaScript, the number type cannot represent integer values larger than 253 (or less than -253 for negatives).
That’s about 16 decimal digits. If you need a higher value you must use BigInt.

A BigInt is created by appending n to the end of an integer literal or by calling the function BigInt().
const theBiggestInt = 9007199254740991n

JavaScript Strings

Strings are written with single or double-quotes.

JavaScript String includes() Method

The includes() method determines whether a string contains the characters of a specified string.
This method returns true if the string contains the characters, and false if not.

Javascript Hackerrank Switch example

Javascript Conditional Statements Hackerrank Switch example

function getLetter(s) {
    let letter;
    // Write your code here
    switch (true) {
        case 'aeiou'.includes(s[0]):
            letter = 'A';
        case 'bcdfg'.includes(s[0]):
            letter = 'B';
        case 'hjklm'.includes(s[0]):
            letter = 'C';
            letter = 'D';
    return letter;

JavaScript Booleans

Booleans can only have two values: true or false.

Javascript Undefined

In JavaScript, a declared variable without a value, has the value undefined. The type is also undefined.
var number; // values is undefined, type is undefined

Any variable can be emptied, by setting the value to undefined.
var number = undefined;

Empty Values

An empty value has nothing to do with undefined. For example,
var name = “”; // The value is “”, the typeof is “string”

JavaScript Null

In JavaScript null is “nothing”. It is supposed to be something that doesn’t exist.
In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
It is just a special value that represents “nothing”, “unknown value” or “empty”.

You can empty an object by setting it to null

Difference Between Undefined and Null

undefined and null are equal in value but different in type:

typeof undefined // undefined
typeof null // object

null === undefined // false
null == undefined // true

JavaScript Simbols

Symbols are values that programs can create and use as property keys without risking name collisions.

var mySymbol = Symbol();

Calling Symbol() creates a new symbol, a value that’s not equal to any other value.

Just like a string or number, you can use a symbol as a property key.
Because a symbol it’s not equal to any string, this symbol-keyed property is guaranteed not to collide with any other property.

obj[mySymbol] = “abcd!”; // guaranteed not to collide
console.log(obj[mySymbol]); // abcd!

JavaScript Object Types

Everything else that is not a Primitive type is an Object type.

JavaScript Objects

JavaScript objects are written with curly braces {}.
var car = {color:”red”, type:”SUV”};

Read more here about JavaScrip objects.

JavaScript Arrays

JavaScript arrays are written with square brackets and items are separated by commas.
var names = [“Mike”, “Will”, “John”];
Array indexes are zero-based

The typeof Operator

The typeof operator returns the type of a variable or an expression:

typeof “” // Returns “string”
typeof “color” // Returns “string”

Be careful!

  • The typeof operator returns “object” for objects, arrays, and null.
  • The typeof operator return “functio” for functions.

JavaScript Array Methods


toString() converts an array to a string


pop() method removes the last element from an array


push() method adds a new element to an array (at the end)


sort() method sorts an array alphabetically

Be careful when trying to sort numerically arrays
var num = [1,3,10,4,5];
console.log(num.sort()); //[ 1, 10, 3, 4, 5 ]

Because JavaScript sorts alphabetically. This means that “10” is lower than “4” because “1” is lower than “4”

How to sort numeric array in javascript?

Here is the syntax of the sort method in javascript:

When compareFunction is not provided:

When compareFunction is not provided, the default mode of sorting is followed wherein the elements of the array are converted into strings and than compared according to the UTF 16 code unit values and the elements are sorted in ascending order by default.

When compareFunction is provided:

When compareFunction is provided, then the function takes over the sorting process and all the non-undefined elements get sorted based on the return value of compareFunction according to the following logic.

Sort a numeric array in ascending order

sort(function(a,b){return a-b});

Sort a numeric array in descending order

sort(function(a,b){return b-a});

Hackerrank Example – Return the value of the second largest number in a numeric array

nums = [ Set(nums)];                // Delele Duplicates
nums.sort(function(a,b){return b-a});    // Sort Array in reverse order
return nums[1]                           // Select 2 item

Set objects are collections of unique values. Set can store any types of values whether primitive or objects. Read more here about Sets in JavaScript

JavaScript Declarations are Hoisted

In JavaScript, a variable can be declared after it has been used, meaning the variable can be used before it has been declared.
Hoisting is the JavaScript default behavior of moving all declarations to the top of the current scope (the current script or the current function).

Be careful! JavaScript Initializations are Not Hoisted!
JavaScript only hoists declarations, not initializations.

Javascript variables

var x = 5;
x = 1;
console.log(x); //Return 1

JavaScript Const

ES2015 introduced two new JavaScript keywords: let and const.
Variables defined with const behave like let variables, except they cannot be reassigned:

const x = 5;
x = 1;
console.log(x); //This will give an error

Block Scope
Declaring a variable with const is similar to let when it comes to Block Scope.
The x declared in the block, in this example, is not the same as the x declared outside the block:

 var x = 5;
// Here x is 5
  const x = 1;
  // Here x is 1
// Here x is 1 

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Reply

Your email address will not be published. Required fields are marked *