Loading Now
×

What is a variable in JavaScript?

What is a variable in JavaScript?

The variable is like a container where you can store something. In JavaScript something means, you can say a lot. You can save numbers, strings, objects, even functions in a  variable. A variable is like, suppose you’re saying ‘David is an honest person he is working on the farm’. Here, you see, first, you mentioned David’s name but then you didn’t mention the second time, it became ‘he’. This variable is similar to this. For example 

A = David 

A is from the USA 

In JavaScript, variables work in the same way. And wherever, whenever you need, you can access the value of that variable. However, you must declare the variable before using it. In JavaScript, variables are declared using the ‘var’ keyword. Regardless of the type of variable, it is declared using ‘var’. After writing ‘var’, you give the name of the variable. You can give any name to the variable. However, some reserved keywords in JavaScript are excluded. 

To declare a variable, you will need five things. First, you will need to use the “var” keyword. After that, you will need to write it. Then you will need to give it a valid name, followed by an equal sign. Then you will need to provide a value. The value can be an object, a function, a string, a number, or a boolean (“true” or “false”). And at last, a semicolon  Here are some examples of variables:

var name = “devid” ; — string 

var  number = 10 ; — number 

var ishappy = true ;– boolean 

Number type variable 

It’s a simple variable. You need to just store a number 

Like, var price = 100 ; 

When you need to access the variable, you can just write the name of the variable and get the value for future use. It is called a numeric variable.

String type variable 

This is also variable, so we will start with ‘var’, but it will be slightly different. When you assign a value to a variable that can be composed of two or more words, it should be enclosed within quotes. If you use single quotes at the beginning, you should also use single quotes at the end. If you use double quotes at the beginning, then you should also use double quotes at the end. For example 

Var myName = “Jason Statham” ;

Var herName = ‘Eva green’ ; 

Boolean type variable 

There is another type of variable. If you ask someone, ‘Are you doing well?’ the answer could be yes or no. Then again, ‘Are you happy?’ the answer could be yes or no. Finally, ‘Did you take money from your father?’ the answer could be yes or no. That means this is another type of variable whose value is expressed as true or false.

Var serious = true ;

Var isFullMarkes = true ;

Var isSingle = false ;

Variable naming conventions

Space cannot be used in variable names. If necessary, “_” underscore can be used instead. In JavaScript, variable names are case sensitive, meaning Myname, myname, myName are all different variables. You must access them exactly as they are named, whether in uppercase or lowercase. The name of the variable can start with a letter, but it cannot start with numbers or special characters ! @ # % ^ & *. However, it can start with $.

A variable name cannot be any keyword, a variable name has to be in one word,

Variable names cannot have quotations, and variable name cannot start with a number but end with a number 

How to use the long name 

Var userHomeAddress = “New California” ;

This method is called the camel case. You can write all the words together here, but the first letter of the first word will be in lowercase, and the first letter of all the other words will be in uppercase. 

Pascal case 

Var UserHomeAdress = “New California”

The first letter of each word will be capitalized here, and all words will be together.

Snake Case 

Var user_home_address = “New york”

In this context, you can write variable names with “_” underscore in the middle of each word.

however, the camel case is the most popular

Post Comment