DEV Community

Santhosh Kumar
Santhosh Kumar

Posted on

1 1

JAVASCRIPT: DATA TYPES

This post was originally published on my blog, find original post here

Every variable has a data type that determines type of value it stores, a variable of type number can store an integer or floating-point numbers.

JavaScript is dynamically typed language which means a variable can be number type for a moment and at another be a string type.

Number

number type represents both integer and fractional (floating point numbers).

let num = 79; // integer
let num = 65.45; // floating-point

We can perform arithmetic operations like +, -, *, /.

let num = 45 + 2 * 6; // 57

For very big number or very small number, we can use e (exponent)

let speedOfLight = 2.998e8;

Infinity represents mathematical infinity, this is a special numeric value.

let infinity = 1 / 0; 
console.log(infinity); // Infinity

NaN is also a special numeric value to represent the incorrect or invalid mathematical operation.

let nan = "str" / 79;

We can get the data type of a variable using typeof.

console.log(typeof "peace"); //string

String

string is used to represent text. They are written by eclosing them their content in quotes.

let hello = "Hello World";
let sq = 'sinle quote';

Both single quote and double quote can be used.

Backticks represents string template, we can embed variable and expression in a string by enclosing them in ${ variable or expression }
expression inside ${....} is evaluated and becomes a part of the string.

let name = "Kevin";
console.log(`My name is ${ name } `); // My name is kevin
console.log(`Age is ${17 + 1} `); // Age is 18

Boolean

Boolean type has two values only: true and false.

It is used to store yes/ no values.

let isGreater = true;

null

To represent nothingness, empty, we can use null

let answer = null;

undefined

if a value is not assigned to a variable or variable is not declared , it will have undefined .

Meaning of undefined is "value is not assigned".

let a;
console.log(a) //undefined

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

AWS Security LIVE! Stream

Go beyond the firewall

Watch AWS Security LIVE! to uncover how today’s cybersecurity teams secure what matters most.

Learn More

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay