Data types in javascript

yogeshktm

yogeshwaran

Posted on March 24, 2024

Data types in javascript

Javascript is a dynamically typed language. Values are not bound to any of the data type. Javascript itself responsible for handling data types of the values

It is also a weakly typed language, which means it allows implicit type conversion when an operation involves mismatched types, instead of throwing type errors.

const num = 24; // num is a number
/* JavaScript converts num to a string, 
so it can be concatenated with the other operand */
const result = num + "1"; console.log(result); // 241
Enter fullscreen mode Exit fullscreen mode

Primarily there are 7 primitive data types in javascript

1.Number
2.BigInt
3.String
4.Boolean
5.null
6.undefined
7.symbol

1.Number

Number represents both integer **and **floating point numbers. NaN ("Not a Number") is a special kind of number value that's typically encountered when the result of an arithmetic operation cannot be expressed as a number. It is also the only value in JavaScript that is not equal to itself.

let num1 = 245;

Enter fullscreen mode Exit fullscreen mode

2.BigInt

Javascript 'Number' datatype cannot safely represents integers which are larger than 253 - 1 which is 9007199254740991.

We can't store values larger than this in number data type. This data type is used by adding *n * at the end of integer.

In real time we don't required this large numbers in operations.

let largeNum = 9007199254740993n;
Enter fullscreen mode Exit fullscreen mode

3.String

String in javascript must be surronded by single/double quotes or backticks.

Backticks are also known as template literals which is used to concatenate string along with variables.

let str1 = 'This is a string';
let str2 = "This is also a string"
let str3 = `${str1} with backtick` 
// This is also a string with backtick
Enter fullscreen mode Exit fullscreen mode

4.Boolean

Boolean represents **truthiness **of the variable. It is always true or false or 0 or 1.

let x = 1;
let y = 2;
let z = 3;
console.log(x == y); // True
console.log(y == z); // false
Enter fullscreen mode Exit fullscreen mode

5.Null

Special null values does not belong to any of the other data types. Mostly used when there is no value assigned while intialization.

foo; //ReferenceError: foo is not defined
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
Enter fullscreen mode Exit fullscreen mode

6.Undefined

If a variable is declared and not assigned it is said to be undefined.

Technically it is possible to assigned undefined to any variables but it is not recommended to use it. Instead use null and undefined will be reserved for unknown undefined values only.

7.Symbols

Symbol is a built-in object whose constructor returns a symbol primitive — also called a Symbol value or just a Symbol — that's guaranteed to be unique. Symbols are often used to add unique property keys to an object that won't collide with keys any other code might add to the object.

let sym1 = Symbol('cat');
let sym2 = Symbol('cat);
console.log(sym1 === sym2); //False

Enter fullscreen mode Exit fullscreen mode

typeof operator in javascript

typeof is a operator which is used to find datatype of any value.

let a = 22;
let b = 'test';
let c = '22';
console.log(typeof a); // number 
console.log(typeof b); // string
console.log(typeof c); // string 

Enter fullscreen mode Exit fullscreen mode

Hope you are started refreshing your javascript skills. follow me and subscribe for more posts like this. Feel free to comment any suggestions or doubts

💖 💪 🙅 🚩
yogeshktm
yogeshwaran

Posted on March 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Data types in javascript
javascript Data types in javascript

March 24, 2024