Rio's JavaScript Lessons

Courses

Lesson 5: Basic Data Types

In this lesson, you will learn about the basic data types in JavaScript, which are the foundation for storing and manipulating data in a program. You will learn about the different data types available in JavaScript, how to create and initialize variables, and how to perform basic operations with each data type. By the end of this lesson, you will have the knowledge and skills to start working with data in your own JavaScript programs.

Numbers

In JavaScript, there are two main types of numbers: integers and floating-point numbers. Integers are whole numbers, such as 1, 2, and 3. Floating-point numbers are numbers with decimal places, such as 1.1, 2.2, and 3.3. You can create and initialize variables with numeric values using the following syntax:

1// Create and initialize an integer variable 2let myInt = 123; 3 4// Create and initialize a floating-point variable 5let myFloat = 3.14;

You can perform basic arithmetic operations with numeric variables, such as addition, subtraction, multiplication, and division, using the following syntax:

1// Perform arithmetic operations with numeric variables 2let result = myInt + myFloat; 3console.log(result);

Strings

In JavaScript, strings are sequences of characters, such as "Hello, world!" or "JavaScript is fun!". You can create and initialize variables with string values using the following syntax:

1// Create and initialize a string variable 2let myString = "Hello, world!";

You can concatenate (combine) two or more strings using the `+` operator, and you can access individual characters in a string using bracket notation, such as `myString[0]` to access the first character in the string. Here is an example:

1// Concatenate strings and access characters in a string 2let concatenatedString = myString + "JavaScript is fun!" 3console.log(concatenatedString); 4console.log(myString[0]);

Arrays

In JavaScript, an array is an ordered collection of elements, such as `[1, 2, 3]` or `["red", "green", "blue"]`. You can create and initialize variables with array values using the following syntax:

1// Create and initialize a list variable 2let myArray = [1, 2, 3];

You can access individual elements in an array using bracket notation, such as `myArray[0]` to access the first element in the array. You can also modify the elements in an array using bracket notation, such as `myArray[0] = 4 to change the first element in the array to 4. Here is an example:

1// Access and modify elements in a list 2console.log(myArray[0]); 3myArray[0] = 4; 4console.log(myArray);