How to declare a variable type in JavaScript?

Data TypesDescriptionExample

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
4represents textual data
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
5,
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
6 etc
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
7an integer or a floating-point number
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
8,
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
9,
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
0 etc.
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
1an integer with arbitrary precision
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
2 ,
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
3 etc.
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
4Any of two values: true or false
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
5 and
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
6
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
7a data type whose variable is not initialized
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
8
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
9denotes a
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
9 value
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
1
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
2data type whose instances are unique and immutable
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
3
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
4key-value pairs of collection of data
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
5

Here, all data types except

const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
4 are primitive data types, whereas
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
4 is non-primitive.

Note: The

const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
4 data type (non-primitive type) can store collections of data, whereas primitive data type can only store a single data.


JavaScript String

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
4 is used to store text. In JavaScript, strings are surrounded by quotes:

  • Single quotes:
    // BigInt value
    const value1 = 900719925124740998n;
    
    // Adding two big integers
    const result1 = value1 + 1n;
    console.log(result1); // "900719925124740999n"
    
    const value2 = 900719925124740998n;
    
    // Error! BitInt and number cannot be added
    const result2 = value2 + 1; 
    console.log(result2); 
    0
  • Double quotes:
    // BigInt value
    const value1 = 900719925124740998n;
    
    // Adding two big integers
    const result1 = value1 + 1n;
    console.log(result1); // "900719925124740999n"
    
    const value2 = 900719925124740998n;
    
    // Error! BitInt and number cannot be added
    const result2 = value2 + 1; 
    console.log(result2); 
    1
  • Backticks:
    // BigInt value
    const value1 = 900719925124740998n;
    
    // Adding two big integers
    const result1 = value1 + 1n;
    console.log(result1); // "900719925124740999n"
    
    const value2 = 900719925124740998n;
    
    // Error! BitInt and number cannot be added
    const result2 = value2 + 1; 
    console.log(result2); 
    2

For example,

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;

Single quotes and double quotes are practically the same and you can use either of them.

Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with

// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 
3 as shown above.

You will learn about the use of backticks in the JavaScript String tutorial.


JavaScript Number

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
7 represents integer and floating numbers (decimals and exponentials). For example,

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5

A number type can also be

// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 
5,
// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 
6, and
// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 
7 (not a number). For example,

const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN

JavaScript BigInt

In JavaScript,

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
7 type can only represent numbers less than (253 - 1) and more than -(253 - 1). However, if you need to use a larger number than that, you can use the
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
1 data type.

A

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
1 number is created by appending n to the end of an integer. For example,

// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 

Output

900719925124740999n
Uncaught TypeError: Cannot mix BigInt and other types

Note:

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
1 was introduced in the newer version of JavaScript and is not supported by many browsers including Safari. Visit to learn more.


JavaScript Boolean

This data type represents logical entities.

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
4 represents one of two values:
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
5 or
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
6. It is easier to think of it as a yes/no switch. For example,

const dataChecked = true;
const valueCounted = false;

You will learn more about booleans in the JavaScript Comparison and Logical Operators tutorial.


JavaScript undefined

The

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
7 data type represents value that is not assigned. If a variable is declared but the value is not assigned, then the value of that variable will be
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
7. For example,

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

It is also possible to explicitly assign a variable value

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
7. For example,

let name = undefined;
console.log(name); // undefined

Note: It is recommended not to explicitly assign

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
7 to a variable. Usually,
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
9 is used to assign 'unknown' or 'empty' value to a variable.


JavaScript null

In JavaScript,

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
9 is a special value that represents empty or unknown value. For example,

const number = null;

The code above suggests that the number variable is empty.

Note:

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
9 is not the same as NULL or Null.


JavaScript Symbol

This data type was introduced in a newer version of JavaScript (from ES2015).

A value having the data type

const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
2 can be referred to as a symbol value.
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
2 is an immutable primitive value that is unique. For example,

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
0

Though value1 and value2 both contain

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
5, they are different as they are of the
const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN
2 type.

Visit JavaScript Symbol to learn more.


JavaScript Object

An

const dataChecked = true;
const valueCounted = false;
6 is a complex data type that allows us to store collections of data. For example,

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
1

You will learn about JavaScript Objects in later tutorials.


JavaScript Type

JavaScript is a dynamically typed (loosely typed) language. JavaScript automatically determines the variables' data type for you.

It also means that a variable can be of one data type and later it can be changed to another data type. For example,

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
2

JavaScript typeof

To find the type of a variable, you can use the

const dataChecked = true;
const valueCounted = false;
7 operator. For example,

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
3

Notice that

const dataChecked = true;
const valueCounted = false;
7 returned
const dataChecked = true;
const valueCounted = false;
9 for the
const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5
9 type. This is a known issue in JavaScript since its first release.

How do you declare variables in types?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.

How to declare variable type string in JavaScript?

To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or const keyword for its declaration. But for declaring a string variable we had to put the string inside double quotes or single quotes.