Variables and Data Types

In JavaScript, you can declare variables using the var, let, or const keywords. Here’s an example:

let message = "Hello, World!";
console.log(message);

In this code, we’re declaring a variable called message using the let keyword, and initializing it with the string “Hello, World!”. We then pass the message variable as an argument to console.log(), which outputs the value of the variable to the console.

JavaScript supports several data types, including:

  • Numbers: whole numbers or decimals
  • Strings: sequences of characters
  • Booleans: true or false
  • Arrays: ordered collections of values
  • Objects: collections of key-value pairs
  • Null and undefined: special values that indicate the absence of a value

Here are some examples of how to declare and use variables of different data types:

let age = 30; // Number
let name = "John"; // String
let isStudent = true; // Boolean

let numbers = [1, 2, 3, 4, 5]; // Array
let person = { name: "Jane", age: 25 }; // Object

let empty = null; // Null
let notDefined; // Undefined

Once you’ve declared a variable, you can use it in your code in a variety of ways. For example, you might use arithmetic operators to perform calculations with numbers, or use string concatenation to combine strings. Here are some examples:

let x = 10;
let y = 5;

console.log(x + y); // Output: 15

let greeting = "Hello";
let name = "John";

console.log(greeting + " " + name); // Output: "Hello John"

By understanding variables and data types in JavaScript, you’ll be well on your way to building more complex programs and applications.

In the next article, we will discuss differences between let, var and const keyword variables with code examples.

Be the first to comment

Leave a Reply

Your email address will not be published.


*