Data Types

Type of the data which is used to represent is called datatypes.

Example:

"name"
12 //number

Types of often divided into two categories Primitive types and Reference types.

Primitive Types

  • String
  • Number
  • Boolean
  • Undefined
  • Null

Number

The number is a 64-bit floating-point format defined by the IEEE 754 standard. It means you can represent as big as you can as ±1.7976931348623157 × 10308 and as small as ±5 × 10−324.This no integer type which we see in other languages like c and java.

Examples:

1
234
13.40393

String

  • Strings are Unicode characters in JavaScript which means each character takes 16bits or 2bytes.
  • A String type in javascript is represented by using double quotes(” ”) or single quotes(’ ’) both are accepted.

Examples:

'hello'
"hello"

How to write multiline strings in Javascript?

Examples:

'Today I'm \nlearning javascript'

\n prints the string in a new line like in above example learning javascript prints on a new line.

Practice it in below console.

Boolean

Boolean is a type which often has a true or false.where you can add or subtract booleans in javascript true has a number 1 false has number 0.

Examples

true;
false;
true-false;
true+true;

Falsy values in JavaScript

  • ” // empty string literal single quoted
  • "" // empty string literal double quoted
  • 0
  • -0
  • NaN // not a number
  • false
  • null
  • undefined

Undefined

You can see undefined mainly in javascript where you declared a variable but not initialized with any value to it.

Null

Null means nothing or nill.

Reference Types

Referenced types which belong to the type Object.

Objects

Objects can be represented in javascript by using the collection of key-value pairs.

Examples

{
  name:"guest",
  age:01
}

In above example name is the key and value is the “guest”

Arrays

Arrays are also treated as an Object type in javascript.

Formal definition: Array is collection or group of characters or values.

[2,3,4,5]

Functions

Every function in JavaScript starts with a keyword function.

Let’s write our first function we already know every function starts with a keyword function.

Function Declaration

function add(num1,num2){
 return num1+num2; // function body
}

This often called with different names function definition or function statements don’t confuse.

Declaring a function can’t do anything we need to invoke the function.

How to Invoke functions?

To invoke a function we need to type a name of the function and () parenthesis.

add(1,2);

To know more about functions refer to the functions topic.