Variables ๐Ÿš€

ยท

3 min read

Variables ๐Ÿš€

Variables are used to store different types of data like numbers, text, strings or more complex structures.

Much like containers at home where you store various items like cereals, rice etc, variables store the data. later on, we can use that data, we can also change that data.

We see the variable declaration in the previous lesson. Now we will see variable initialization, variable initialization simply means to assign a value to the variable when it's declared.

var name; // Variable declaration
name = "Anjali" ; // variable Intialization 

// In one line 
var name = "Anjali";
var age = 19;

// can change the data that is stored inside name variable.
name = "rajni"
age = 25

Here, semicolon are used to denote the end of a statement But, It is optional in Javascript. But in other programming language it is a must to add semicolon at the end of the statement.

Conventional Rules For Naming Variables In Javascript

In JavaScript, there are conventional rules for naming variables to ensure readability and consistency in your code. Here are some common practices:

  1. Camel Case: This is the most widely used convention in JavaScript. The first letter of the variable name starts with a lowercase letter, and each new word begins with a capital letter. For example: myName

    In Python snake case naming is used like my_name

  2. Meaningful and Descriptive: Variable names should be meaningful and describe the data they hold. This improves code readability. For instance, firstName is more descriptive than fn.

  3. Avoid Reserved Keywords: Don't use JavaScript reserved keywords as variable names. For example, you can't name a variable "var" or "function" as they are reserved words, reserved to declare a variable and function.

  4. Case Sensitivity: JavaScript variable names are case-sensitive. "myVariable" and "myvariable" would be treated as two different variables.

  5. Constants: For constants, it's common to use all uppercase letters with underscores to separate words. For example: MAX_SIZE, PI

var myName="anjali";    ๐Ÿ‘
var function = 5;       ๐Ÿ‘Ž
const PI = 3.14;        ๐Ÿ‘
const MAX_SIZE = 200;   ๐Ÿ‘

Rules For Naming Variables In Javascript

  1. Cannot start with a number: Variables names cannot begin with a number.

  2. Start with a Letter, $, or _ : Variable names must begin with a letter, dollar sign ($), or underscore (_).

  3. Variable names can only include certain characters: letters, digits, underscores (_), and dollar signs ($). Any other special characters are not allowed.

  4. Cannot add spaces: cannot use spaces while naming the variable.

 var 2number = 8; // Incorrect, cannot start with a number.
 var number_2 = 8; // correct , can use underscore.
 var number2$ = 5; // correct , can use dollor. 
 var _name = 6; //correct , can start the name with underscore , dollor and with alphabet
 var $hi@22 = 4; //incorrect , cannot use @ character .
 var first name = "yashika"; // incorrect , spaces are not allowed

In the next lesson, we will talk about the strict mode in JavaScript and will discuss some basic differences between let and var.

ย