27.4 C
Gujarat
HomeJavascriptvariables and values in javascript

variables and values in javascript

Variable means anything that can vary. In JavaScript, variables hold the data value and it can be changed anytime. 

JavaScript uses reserved keyword var to declare a variable. A variable must have a unique name. You can assign a value to a variable using equal to (=) operator when you declare it or before using it.


Syntax :

var variable-name; 

var variable-name = value;

Example: Variable Declaration


var myName="patel";  //variable stores string value
var myAge=22;      // variable stores number value
var city;          //declared a variable without assigning a value

variables and values in javascript


Naming Variables Rules:

1)The first character must be a letter or an underscore(_) or a dollar($). You can’t use a number as the first character

Example:  


var myName="patel";  //valid
var _myName="rocoderes";   //valid
var $myAge=21;  //valid


var 1myname="Beast"; //not valid

2)The rest of the variable name can include any letter, any number, or the underscore. can’t use any other character, including spaces.

Example:


var _myAge1=22;  //valid
var my Age=23;   //not valid

3)Variable names are case sensitive


Example:

var myAge=22;
console.log(myage); //its show ReferenceError: myage is not defined

4)no limit to the length of the variable name. 

5)you can’t use one of the javascript reserved words as a variable name.

Example:


var var=22;  //not valid

Challenge Time


var _myName="patel";
var _1my__name="ro";
var 1myname="coderes";
var $myName="Rocoderes";

try it yourself whether it is valid or not

Previous articleBasic Git Commands
Next articleData types in javascript
RELATED ARTICLES

2 COMMENTS

  1. Hello! This post couldn’t be written any better! Reading this
    post reminds me of my old room mate! He always kept talking about this.

    I will forward this article to him. Pretty sure he will have a good read.
    Thanks for sharing!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15,000FansLike
5,000FollowersFollow
535FollowersFollow
- Advertisment -spot_img

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Most Popular