JavaScript tutorials for Beginners -JavaScript Data Types

 

 

(JavaScript Tutorials for Beginners)

In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Data Types.

 

JavaScript Data Types

In this tutorial, you will learn about the various data types available in JavaScript with the help of examples.

As suggested by the name data types, it refers to types of data that you can use in your program. For example,

let x = 5;
let y = "Hello";

Here,

  • 5 is an integer data.
  • “Hello” is a string data.

JavaScript Data Types

There are eight basic data types in JavaScript. They are:

Data Types Description Example
String represents textual data 'hello', "hello world!" etc
Number an integer or a floating-point number 3, 3.234, 3e-2 etc.
BigInt an integer with arbitrary precision 900719925124740999n , 1n etc.
Boolean Any of two values: true or false true and false
undefined a data type whose variable is not initialized let a;
null special keyword denoting a null value let a = null;
Symbol data type whose instances are unique and immutable let value = Symbol('hello');
Object key-value pairs of collection of data let student = { };

Here, all data types except Object are primitive data types, whereas Object is non-primitive.

Note: The Object data type (non-primitive type) can store collections of data, whereas primitive data type can only store a single data.


JavaScript String

String is used to store text. In JavaScript, strings are surrounded by quotes:

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Backticks: `Hello`

For example,

//strings example
let name = 'ram';
let name1 = "hari";
let 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 ${variable or expression} as shown above.

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


JavaScript Number

Number represents integer and floating numbers (decimals and exponentials). For example,

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

A number type can also be +Infinity-Infinity, and NaN (not a number). For example,

let number1 = 3/0;
console.log(count); // returns Infinity

let number2 = -3/0;
console.log(count1); // returns -Infinity

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

JavaScript BigInt

In JavaScript, Number 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 BigInt data type.

BigInt number is created by appending n to the end of an integer. For example,

// BigInt value
let value = 900719925124740998n;

// Adding two big integers
let value1 = value + 1n;
console.log(value1); // returns "900719925124740999n"

let value = 900719925124740998n;

// Error! BitInt and number cannot be added
let value1 = value + 1; 
console.log(value1); 

Output

900719925124740999n
Uncaught TypeError: Cannot mix BigInt and other types

Note: BigInt was introduced in the newer version of JavaScript and is not supported by many browsers including Safari. Visit JavaScript BigInt support to learn more.


JavaScript Boolean

This data type represents the logical entity. Boolean represents one of two values: true or false. It is easier to think of it as a yes/no switch. For example,

let dataChecked = true;
let valueCounted = false;

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


JavaScript undefined

The undefined data type represents value is not assigned. If a variable is declared but the value is not assigned, then the value of that variable will be undefined. For example,

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

It is also possible to explicitly assign a variable value undefined. For example,

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

Note: It is recommended not to explicitly assign undefined to a variable. Usually, null is used to assign ‘unknown’ or ’empty’ value to a variable.


JavaScript null

In JavaScript, null is a special value that represents empty or unknown value. For example,

let number = null;

The code above suggests that the number variable is empty.

Notenull 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 Symbol can be referred to as a symbol valueSymbol is an immutable primitive value that is unique. For example,

// two symbols with the same description

let value1 = Symbol('hello');
let value2 = Symbol('hello');

Though value1 and value2 both contain 'hello', they are different as they are of the Symbol type.

Visit JavaScript Symbol to learn more.


JavaScript Object

An object is a complex data type that allows us to store collections of data. For example,

let student = {
firstName: 'ram',
lastName: null,
class: 10
};

You will learn about JavaScript Objects in the later tutorial.


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,

// data is of undefined type
let data;

// data is of integer type
data = 5;

// data is of string type
data = "JavaScript Programming";

JavaScript typeof

To find the type of a variable, you can use the typeof operator. For example,

let name = 'ram';
typeof(name); // returns "string"

let number = 4;
typeof(number); //returns "number"

let valueChecked = true;
typeof(valueChecked); //returns "boolean"

let a = null;
typeof(a); // returns "object"

Notice that typeof returned "object" for the null type. This is a known issue in JavaScript since its first release.

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!

 

Learn Java by Example: Java Program to convert string type variables into boolean

Learn Java by Example: Java Program to convert boolean variables into string

Python Built-in Methods – Python globals() Function