Mark As Completed Discussion

Working with Variables and Data Types

In JavaScript, variables are used to store and manipulate data. They can hold different types of values, such as numbers, strings, booleans, arrays, and objects. To declare a variable, you can use the let or const keyword.

JAVASCRIPT
1// Declare a variable and assign a value
2let name = 'John';
3
4// Declare a constant variable
5const age = 30;

Primitive Data Types

JavaScript has several primitive data types:

  • Number: Used for numeric values, such as 10, 3.14, or 5.67e8.
  • String: Used for text values, enclosed in single or double quotes, such as 'Hello' or 'World'.
  • Boolean: Used for logical values, either true or false.

Arrays

An array is a special type of variable that can hold multiple values. Each value in an array is called an element, and elements are accessed using index numbers, starting from 0.

JAVASCRIPT
1let numbers = [1, 2, 3, 4, 5];
2console.log(numbers[0]); // Output: 1
3console.log(numbers[2]); // Output: 3

Objects

An object is a collection of key-value pairs. Each key in an object is a unique identifier, and its corresponding value can be any data type.

JAVASCRIPT
1let person = {
2  name: 'John',
3  age: 30,
4};
5console.log(person.name); // Output: John
6console.log(person.age); // Output: 30

By understanding how to declare variables and work with different data types, you'll be able to manipulate and store data effectively in JavaScript.