|
1 | | -# Variables |
| 1 | +# Variables |
| 2 | + |
| 3 | +## Datatypes |
| 4 | +One of the most fundamental characteristics of a programming language is the set of data types it |
| 5 | +supports. These are the type of values that can be represented and manipulated in a |
| 6 | +programming language. |
| 7 | +JavaScript allows you to work with three primitive data types − |
| 8 | +Numbers, eg. 123, 120.50 etc. |
| 9 | +Strings of text e.g. "This text string" etc. |
| 10 | +Boolean e.g. true or false. |
| 11 | +JavaScript also defines two trivial data types, null and undefined, each of which defines only a |
| 12 | +single value. In addition to these primitive data types, JavaScript supports a composite data type |
| 13 | +known as object. We will cover objects in detail in a separate chapter. |
| 14 | +Note − Java does not make a distinction between integer values and floating-point values. All |
| 15 | +numbers in JavaScript are represented as floating-point values. JavaScrip |
| 16 | + |
| 17 | +Like many other programming languages, JavaScript has variables. Variables can be thought of as |
| 18 | +named containers. You can place data into these containers and then refer to the data simply by |
| 19 | +naming the container. |
| 20 | +Before you use a variable in a JavaScript program, you must declare it. Variables are declared with |
| 21 | +the var keyword as follows. |
| 22 | +```js |
| 23 | +<script type="text/javascript"> |
| 24 | + var money; |
| 25 | + var name; |
| 26 | +</script> |
| 27 | +``` |
| 28 | +You can also declare multiple variables with the same var keyword as follows − |
| 29 | +```js |
| 30 | +<script type="text/javascript"> |
| 31 | +<!-- |
| 32 | + var money, name; |
| 33 | +//--> |
| 34 | +</script> |
| 35 | +``` |
| 36 | +Storing a value in a variable is called variable initialization. You can do variable initialization at |
| 37 | +the time of variable creation or at a later point in time when you need that variable. |
| 38 | +For instance, you might create a variable named money and assign the value 2000.50 to it later. |
| 39 | +For another variable, you can assign a value at the time of initialization as follows. |
| 40 | +```js |
| 41 | +<script type="text/javascript"> |
| 42 | +<!-- |
| 43 | + var name = "Ali"; |
| 44 | + var money; |
| 45 | + money = 2000.50; |
| 46 | +//--> |
| 47 | +</script> |
| 48 | +``` |
| 49 | +Note − Use the var keyword only for declaration or initialization, once for the life of any variable |
| 50 | +name in a document. You should not re-declare same variable twice. |
| 51 | +JavaScript is untyped language. This means that a JavaScript variable can hold a value of any |
| 52 | +data type. Unlike many other languages, you don't have to tell JavaScript during variable |
| 53 | +declaration what type of value the variable will hold. The value type of a variable can change |
| 54 | +during the execution of a program and JavaScript takes care of it automatically. |
| 55 | +JavaScript Variable Scope |
| 56 | +The scope of a variable is the region of your program in which it is defined. JavaScript variables |
| 57 | +have only two scopes. |
| 58 | +Global Variables − A global variable has global scope which means it can be defined |
| 59 | +anywhere in your JavaScript code. |
| 60 | +Local Variables − A local variable will be visible only within a function where it is defined. |
| 61 | +Function parameters are always local to that function. |
| 62 | +Within the body of a function, a local variable takes precedence over a global variable with the |
| 63 | +same name. If you declare a local variable or function parameter with the same name as a global |
| 64 | +variable, you effectively hide the global variable. Take a look into the following example. |
| 65 | +```js |
| 66 | +<script type="text/javascript"> |
| 67 | +<!-- |
| 68 | + var myVar = "global"; // Declare a global variable |
| 69 | + function checkscope( ) { |
| 70 | + var myVar = "local"; // Declare a local variable |
| 71 | + document.write(myVar); |
| 72 | +} |
| 73 | +//--> |
| 74 | +</script> |
| 75 | +``` |
| 76 | +This produces the following result − |
| 77 | +local |
| 78 | +JavaScript Variable Names |
| 79 | +While naming your variables in JavaScript, keep the following rules in mind. |
| 80 | +You should not use any of the JavaScript reserved keywords as a variable name. These |
| 81 | +keywords are mentioned in the next section. For example, break or boolean variable names |
| 82 | +are not valid. |
| 83 | +JavaScript variable names should not start with a numeral 0 − 9. They must begin with a letter |
| 84 | +or an underscore character. For example, 123test is an invalid variable name but _123test |
| 85 | +is a valid one. |
| 86 | +JavaScript variable names are case-sensitive. For example, Name and name are two |
| 87 | +different variables. |
0 commit comments