JavaScript Variables

In JavaScript, variables are used to store values that can be referenced and manipulated in a program. A variable is declared using the var, let, or const keyword, followed by the variable name, and optionally initialized with a value.

Here are the examples of variable declarations in JavaScript

var name = "John"; // declares a variable named "name" with the value "John"
let age = 30; // declares a variable named "age" with the value 30
const PI = 3.14159; // declares a constant variable named "PI" with the value 3.14159

JavaScript Data Types

In JavaScript, there are several types of data that can be stored in variables. These data types include:

String

A string is a sequence of characters, enclosed in single or double quotes. Example: "Hello, World!".

Booleans

A boolean is a value that can be either true or false. Example: true.

Array

An array is a collection of values, enclosed in square brackets and separated by commas. Example: [1, 2, 3].

Object

An object is a collection of key-value pairs, enclosed in curly braces. Example: {name: "John", age: 30}.

Here are some examples of variables with different data types

var name = "John"; // string variable
let age = 30; // number variable
const isStudent = true; // boolean constant variable
let numbers = [1, 2, 3, 4]; // array variable
const person = {name: "John", age: 30}; // object constant variable

JavaScript Comments

Comments are used to add explanatory notes or remarks within the code that are not executed by the JavaScript engine. Comments are ignored by the browser and are intended for human readers to understand the code better.

There are two types of comments in JavaScript: single-line comments and multi-line comments.

Single-line comments

Single-line comments start with // and continue until the end of the line. They are typically used for short comments.

Multi-line comments

Multi-line comments start with /* and end with */. They can span multiple lines and are often used for longer explanations or for temporarily disabling code.

Here are the examples on how to use comments in JavaScript

// This is a single-line comment.
const greeting = "Hello, World!"; // Assigning a value to the variable 'greeting'.

//Below is example of multi-line comment
/*
This is a multi-line comment.
It can span multiple lines and is ignored by the JavaScript engine.
*/

/* 
const x = 5;
console.log(x);
This code is commented out and will not be executed.
*/

Conclusion

Overall, variables are used to store values that can be referenced and manipulated in a program. A variable is declared using the var, let, or const keyword, followed by the variable name, and optionally initialized with a value. There are several types of data that can be stored in variables in JavaScript, including strings, numbers, booleans, arrays, and objects. Lastly, comments helps the developer or reader understand the purpose and functionality of different parts of the code.

Now challenge yourself, click the link below to practice!

  • https://codepen.io/pen/