JavaScript Variables
Last Updated Jul 29, 2015, 07:00:14 PM
JavaScript Variables
A variable is a way of storing and keeping track of information in a program. For example, a computer game keeps track of a player's score. At the start of the game, the score is zero. It can go up if the player does well or go down if the player makes a mistake. The game might even end if the player reaches a particular score. The score is an example of a variable. Although the score's value will change, zero at the beginning, 100 at the end, for example, it's still always just one score for that player.
A JavaScript identifier must start with a letter, underscore (_), or a dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Declaring JavaScript variables
You can declare javascript variable in three different ways:
Using var
keyword .
This syntax can be used to declare both local and global variables.
Try It NowVariables in Javascript behave same as most popular languages like c, c++ and other languages variables. The only difference from other languages is, JavaScript variable is not mandatory to define the datatype of the variable
. JavaScript will take care of all types even if you do not want define data type.
By Simply Assigning
ExampleTry It Now
This always declares a global variable and cannot be changed at the local level. It generates a strict JavaScript warning. You shouldn't use this variant.
Using Keyword let
ExampleTry It Now
This syntax can be used to declare a block scope local variable
Declaring JavaScript Variables
JavaScript variables can be delcared using the variable name like below
SyntaxIf you notice the above example the variable has no value. Technically it has the value of undefined
To assign a value to the variable, use the equal sign:
Declaring more than one variable
You can also declare more than one variable in the same line like below
Try It Now
JavaScript Identifiers
JavaScript Identifiers are named; names that you give things in JavaScript. These JavaScript “things†include
- variables
- functions
- objects
- properties
- methods
- events
There are some rules to follow while using the JavaScript language
- Identifiers can only contain letters, numbers, underscore (_) and the dollar sign ($).
- Identifiers cannot start with a number.
- Identifiers are case-sensitive.
- Identifiers can be any length.
- Identifiers can not be the same as JavaScript reserved words. (See list of reserved words)
- Don't use global properties and methods as identifiers (more later).
- Don't use words similar to reserved words.
When naming an identifier with two words in it, it's a best practice to use camel case. With this convention, the first letter of each word, excluding the first word, is uppercase. Example:
Here are some examples of valid identifier naming conventions:
Note : JavaScript identifiers are case-sensitive.
JavaScript Data Types
JavaScript variables can hold different data types like numbers, string, and complex data types as well. In this javascript language, we call text related values as strings.
Lets look at the how numbers and strings can be used in JavaScript program. Here strings are written within the single or double quotes whereas numbers can be written without quotes.
Note:numbers can be treated as strings if you mention quotes around it.
Let's see some example of numbers and strings
Try It Now
Declaring JavaScript Variables before using it
As a JavaScript developer, you can always declare variables before you use them. Variables don't have to declare before being used
ExampleTry It Now
When JavaScript sees a declared identifier (var varName), it creates a global variable.
Reassigning value to Variables
JavaScript gives more flexibility to reassign or change the value of a variable anywhere in the middle of the program. When the program runs the latest value gets executed
ExampleTry It Now
Practice with our Interactive Editor and take your JavaScrpt Skills to the next level
Exercise 1 Exercise 2 Exercise 3 Exercise 4 Exercise 5
Sources and Credits
The source of the content has been referred and updated with Mozilla Developer Network and W3C Organization
Last Updated Jul 29, 2015, 07:00:14 PM
Complete Beginner to JavaScript Developer - Udemy
Advanced JavaScript Development - Udemy
Learn javascript in one month from experts -OneMonth
Become a Certified Web Developer - Udemy