There are different data types in JavaScript:
- number
- strings
- boolean
- null & undefined
- objects
Let's discuss one-by-one
- The
number
data type in JavaScript represents numeric values. - Numbers can be integers (whole numbers) or floating-point numbers (decimal numbers).
Here are some examples of number
data types in JavaScript:
const integer = 56;
const floatingPoint = 0.59;
const negativeNumber = -2;
- In addition to these basic examples, JavaScript provides a variety of arithmetic operations that can be performed on
number
data types, such as addition (+
), subtraction (-
), multiplication (*
), and division (/
).
Here are some examples of arithmetic operations with number
data types in JavaScript:
const sum = 2 + 2; // 4
const difference = 5 - 3; // 2
const product = 4 * 6; // 24
const quotient = 10 / 2; // 5
JavaScript also provides several special number
values:
Infinity
: Represents the mathematical concept of infinity, which is a value greater than any other number.-Infinity
: Represents the mathematical concept of negative infinity, which is a value less than any other number.NaN
: Represents "Not a Number", which is a special value that indicates an invalid mathematical operation.
Here are some examples of special number
values in JavaScript:
const positiveInfinity = Infinity;
const negativeInfinity = -Infinity;
const notANumber = "hello" / 2; // NaN
- One important thing to note is that JavaScript has a "loose" type system, which means that it can sometimes be difficult to determine the type of a variable.
For example:
onst numberAsString = "42";
const result = numberAsString + 2;
console.log(result); // "422"
Explanation : In the above example, numberAsString
is a string
data type, but because we use the +
operator to add 2
to it, JavaScript converts numberAsString
to a number
data type in order to perform the addition. The result is the string
value "422"
, not the number
value 44
.
Overall, the number
data type is a fundamental part of JavaScript and is used extensively in mathematical operations and other areas of programming.
- In JavaScript, a
string
is a sequence of characters that is used to represent text. - Strings are enclosed in single or double quotation marks.
Here are some examples of string
data types in JavaScript:
const greeting = "Hello, world!";
const firstName = "Siddharth";
const lastName = "Patil";
- In addition to these basic examples, JavaScript provides a variety of string operations that can be performed on
string
data types, such as concatenation (joining two or more strings together), substring extraction, and searching.
Here are some examples of string operations with string
data types in JavaScript:
const fullName = firstName + " " + lastName; // "Siddharth Patil"
const firstLetter = fullName.charAt(0); // "S"
const lastNameSubstring = fullName.substring(9); // "Patil"
const indexOfO = fullName.indexOf("i"); // 1
JavaScript also provides several special string
values:
null
: Represents a deliberate non-value, or absence of any object value.undefined
: Represents an unintentional non-value, which means a variable or property that has not been assigned a value.empty string
: Represents astring
with no characters, represented as""
.
Here are some examples of special string
values in JavaScript:
const nullString = null;
const undefinedString = undefined;
const emptyString = "";
- One important thing to note is that JavaScript provides several ways of representing special characters in strings.
\n
: Represents a new line character.\t
: Represents a tab character.\"
: Represents a double quote character.\'
: Represents a single quote character.
Here is an example of using special characters in a string
data type in JavaScript:
const quote = "He said, \"Hello, world!\"";
console.log(quote); // "He said, "Hello, world!""
- The third way of representing a string in JavaScript is by enclosing it in backticks (```).
- These are also known as template literals, and were introduced in ECMAScript 6 (ES6).
- Template literals allow for string interpolation, which means you can embed expressions within the string. To embed an expression within the string, wrap it in
${}
.
Here's an example:
onst name = "Siddharth";
const templateLiteralString = `My name is ${name}`;
- Template literals can also span multiple lines, which can make code easier to read and write.
Here's an example:
const multiLineString = `
This is a multi-line string
that spans multiple lines.
`;
- In JavaScript, the
boolean
data type is used to represent true/false values. - The
boolean
data type can only have two possible values:true
orfalse
.
Here are some examples of boolean
data types in JavaScript:
const isRaining = true;
const isSunny = false;
true
andfalse
are special reserved keywords in JavaScript and they are the only values that can be assigned to aboolean
data type.- The
boolean
data type is often used in conditional statements to control the flow of a program. For example, theif
statement in JavaScript uses aboolean
expression to determine whether a block of code should be executed.
const isRaining = true;
if (isRaining) {
console.log("Don't forget to bring an umbrella!");
}
Explanation : In this example, the if
statement checks whether isRaining
is true
, and if it is, it executes the block of code inside the curly braces.
- In addition to the
true
andfalse
keywords, JavaScript also provides several ways of converting other data types toboolean
values. The following values are considered to be falsy and will convert tofalse
when used in aboolean
context:false
0
""
(empty string)null
undefined
NaN
- Any other value, including non-empty strings, non-zero numbers, and non-null objects, will convert to
true
when used in aboolean
context.
Here are some examples of converting other data types to boolean
values:
Boolean(0); // false
Boolean("hello"); // true
Boolean(null); // false
Boolean({}); // true
- Overall, the
boolean
data type is a fundamental part of JavaScript and is used extensively in controlling program flow and implementing logical operations.
- In JavaScript,
null
is a primitive data type that represents the intentional absence of any object value. null
is often used to represent a variable or object property that is explicitly empty, rather than having an undefined or unknown value.
Here's an example of using the null
value in JavaScript:
let myVar = null;
console.log(myVar); // output: null
Explanation : In this example, the variable myVar
is assigned the null
value, which indicates that it has no object value.
-
It's important to note that
null
is a primitive data type, meaning that it is not an object and has no properties or methods. Trying to access properties or methods on anull
value will result in aTypeError
. -
In JavaScript,
undefined
is a special value that is used to represent an uninitialized or missing value. -
A variable that has been declared but not assigned a value will have the value
undefined
.
Here's an example of using the undefined
value in JavaScript:
let myVar;
console.log(myVar); // output: undefined
Explanation : In this example, the variable myVar
has been declared but not assigned a value, so it has the value undefined
.
undefined
can also be explicitly assigned to a variable or object property, in which case it represents a missing value.
let myVar = undefined;
console.log(myVar); // output: undefined
- It's important to note that
undefined
is also a primitive data type, and trying to access properties or methods on anundefined
value will result in aTypeError
.
- In JavaScript, an
object
is a complex data type that represents a collection of related data or functionality. - An object can contain properties, which are key-value pairs that represent the object's state or attributes, as well as methods, which are functions that represent the object's behavior or actions.
Here's an example of an object in JavaScript:
const person = {
firstName: 'Siddharth',
lastName: 'Patil',
age: 30,
isStudent: true,
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
};
Explanation : In this example, the person
object has four properties (firstName
, lastName
, age
, and isStudent
) and one method (getFullName()
).
- Properties in an object are accessed using dot notation or bracket notation.
Here's an example of accessing the firstName
property of the person
object using dot notation:
console.log(person.firstName); // output: "Siddharth"
And here's an example of accessing the firstName
property of the person
object using bracket notation:
console.log(person['firstName']); // output: "Siddharth"
- In addition to creating objects using object literals, you can also create objects using constructor functions or object classes in JavaScript.
Here's an example of creating an object using a constructor function:
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
const person = new Person('Siddharth', 'Patil', 30);
console.log(person.firstName); // output: "Siddharth"
Explanation : In this example, the Person
function is a constructor function that creates new Person
objects with the specified firstName
, lastName
, and age
properties. The new
keyword is used to create a new Person
object and assign it to the person
variable.