Series: JavaScript 101
- About JavaScript 101
- Hello World in Web Browser
- Hello World in Node.js
- Variable in JavaScript
- Data Types in JavaScript
- Array in JavaScript
- Object in JavaScript
- Spread Operator in JavaScript
- Loops in JavaScript
- Conditional Statements in JavaScript
- Functions in JavaScript
- Object-Oriented Programming in JavaScript
When learning to code in JavaScript, it's important to understand the different types of data that can be used and stored in your program. In JavaScript, we have several data types including numbers, strings, booleans, and more.
In this article, we will go over the different data types in JavaScript and give examples of how they are used in a program. Whether you're a complete beginner or just need a refresher, this article will provide a solid foundation for your understanding of data types in JavaScript.
JavaScript, like most programming languages, has a variety of data types that can be used to store different types of information. These data types include:
Number
This data type can store both integers and floating point numbers. For example, 3 and 3.14 are both considered numbers in JavaScript.
const num = 3;
String
This data type is used to store a series of characters, such as words or sentences. Strings are enclosed in quotation marks (either single or double). For example, "Hello World"
and 'Hello World'
are both valid strings.
const greeting = "Hello World!";
Boolean
This data type can only store two values: true or false. It is often used in conditional statements to check if a certain condition is met.
const value = true;
Array
This data type is used to store a collection of elements, such as a list of numbers or strings. Elements within an array are enclosed in square brackets and separated by commas. For example, [1, 2, 3]
and ["apple", "banana", "orange"]
are both valid arrays.
const nums = [1, 2, 3];
const fruits = ["apple", "banana", "orange"];
Object
This data type is used to store a collection of key-value pairs, where the keys are strings and the values can be of any data type. Objects are enclosed in curly braces {}
. For example, {name: "John", age: 30}
is a valid object.
const users = {name: "John", age: 30}
Function
This data type is used to store a block of code that can be executed when called. Functions are declared using the keyword "function" and can take in parameters. For example, function add(a, b) { return a + b; }
is a function that takes in two parameters and returns their sum.
function add(a, b) {
return a + b;
}
const square = (a) => a * a;
null
vs. undefined
null
and undefined
are special values that represent the absence of a value. null
is explicitly assigned to a variable to indicate that it doesn't have a value, while undefined
is the default value of a variable that has been declared but hasn't been assigned a value yet. Both null
and undefined
are falsy values and are often used to check whether a variable has a value or not. Understanding the difference between these two special values is important in writing efficient and correct code.
Nature of Dynamically typed
It's important to note that JavaScript is a dynamically typed language, which means that a variable's data type can change at any point in the code. This can sometimes lead to unexpected behavior, so it's important to keep track of the data types of variables.
let test = 10;
console.log(test) // 10
test = "Hello world!";
console.log(test) // "Hello world!"
Conclusion
Understanding data types in JavaScript is an essential part of becoming a proficient JavaScript developer. Knowing the different data types and their use cases will help you write more efficient and maintainable code.