In this tutorial, we will explore the various data types available in JavaScript. Data types are vital because they determine what values can be stored in the variable and what operations can be performed on it.
By the end of this tutorial, you will understand the different JavaScript data types, how to declare variables of these types, and how to manipulate them.
Basic knowledge of JavaScript syntax is recommended but not compulsory. If you're familiar with other programming languages, the concepts should be easy to grasp.
JavaScript has seven fundamental data types:
In JavaScript, the Number data type can represent both integers and floats. Here's how to declare a number:
let num = 5; // integer
let floatNum = 5.5; // float
Strings represent textual data. They are enclosed within single quotes (' '), double quotes (" "), or backticks ().
let singleQuoteString = 'Hello, World!';
let doubleQuoteString = "Hello, World!";
let templateLiteralString = `Hello, World!`;
Booleans represent one of two values: true or false.
let isJavaScriptFun = true;
let isSkyGreen = false;
Null is a special value that represents "nothing", "empty", or "no value". Undefined means a variable has been declared but has not yet been assigned a value.
let emptyVar = null;
let undefinedVar;
Objects are used to store collections of data and more complex entities. They can hold data of other data types, including other objects, creating deeply nested structures.
let person = {
name: "John",
age: 30,
hobbies: ["reading", "gaming", "coding"]
};
Symbols are a new primitive type in ECMAScript 6. A symbol is a unique and immutable data type and may be used as an identifier for object properties.
let sym = Symbol('Hello');
let num = 10;
console.log(num); // Outputs: 10
We declare a variable num
and assign it the number 10. When we log num
to the console, it outputs 10
.
let string = "Hello, World!";
console.log(string); // Outputs: Hello, World!
We declare a variable string
and assign it the string "Hello, World!". When we log string
to the console, it outputs Hello, World!
.
let person = {
name: "John",
age: 30,
hobbies: ["reading", "gaming", "coding"]
};
console.log(person.name); // Outputs: John
We declare a variable person
as an object. This object has three properties: name
, age
, and hobbies
. When we log person.name
to the console, it outputs John
.
In this tutorial, we've covered the seven fundamental data types in JavaScript: numbers, strings, booleans, null, undefined, symbols, and objects. We've learned how to declare variables of these types and how to manipulate them.
For further learning, you can explore more complex data structures like arrays and functions, which are special types of objects in JavaScript. You might also want to delve into JavaScript's type coercion and comparison rules.
Declare a variable myNumber
and assign it a number of your choosing. Log myNumber
to the console.
let myNumber = 15;
console.log(myNumber); // Outputs: 15
Declare a variable myString
and assign it a string of your choosing. Log myString
to the console.
let myString = "I'm learning JavaScript!";
console.log(myString); // Outputs: I'm learning JavaScript!
Declare a variable myObject
and assign it an object with properties of your choosing. Log one of the properties to the console.
let myObject = {
color: "blue",
shape: "circle",
size: "large"
};
console.log(myObject.color); // Outputs: blue
Remember, the best way to learn is by doing. Keep practicing and experimenting with different data types and operations.