fbpx
April 6, 2020
javascript varables title

JAVASCRIPT VARIABLES | Introduction to Variables in JavaScript

In this tutorial we will be discussing about JavaScript variables which entails the meaning and usage of variables in JavaScript. In our previous tutorial we discussed about JavaScript placement which involved how and where to place JavaScript codes in a web document. If you haven’t gone through this tutorial you can do so using the link below

Read previous tutorial on: JAVASCRIPT PLACEMENT | Learn How to Place JavaScript in HTML File

JAVASCRIPT DATA TYPES

JavaScript as a programming language supports three different and basic data types which includes:

  • Numbers: values such as 1, 2, 3, 4
  • Strings: values such as “a”, “boy” this are mostly words or text enclosed in single or double quotation marks
  • Boolean: values such as True or False

There are also some trivia data types also supported in JavaScript and they include the null and undefined and also we have composite data type in JavaScript called object

JAVASCRIPT VARIABLES

JavaScript variables can be seen as named containers that holds values. For example before using a variable in JavaScript we must first declare it using the word “var” as shown below

var x – this declares x as a variable that can store data

var y – this declares y as a variable that can store data

var z – this declares z as a variable that can store data

To store a data into any of the variables, we simply assign the variable to the data as shown below

var x = 3 – over here we declared x as a variable and stored 3 in it

var y = 4 – over here we declared y as a variable and stored 4 in it

var z = 8 – over here we declared z as a variable and stored 8 in it

The below example shows a JavaScript code containing declared variables

<script language = "javascript" type = "text/javascript">   
var x;
var y;
var z;
</script>

Another way we can declare multiple variables is shown below

<script language = "javascript" type = "text/javascript">  
   var x,y,z;
</script>

JAVASCRIPT VARIABLE SCOPE

JavaScript variable scope is defined as the region or boundary in which a JavaScript variable is defined. There are two well known variable scopes in JavaScript and they include:

Global variables – this variables are global and can be defined anywhere in your code

Local variables – this variables are not global and can not be placed anywhere in your code. They can only be visible within a function where they are defined.

please note that in JavaScript a local variable declared within the body of a function with the same name as a global variable will take precedence over the global variable. This simple means that if a local variable shares the same name with a global variable and its used within the body of a function, the local variable will be acknowledge while the global variable will be ignored.

<!DOCTYPE html>
<html lang="en">
<head>
   
   <title>checking variable scope</title>
</head>
<body onload="variableScope()" >
   <script language="javascript" type="text/javascript">
   var firstVar= 'global';
   function variableScope(){
      var firstVar= 'local'; 
      document.write(firstVar);
   }
   </script>
</body>
</html>

RESULT

Rules For Naming Variables in JavaScript

The below rules must be observed when naming variables in JavaScript and they include:

  • variables can not start with a number. They must start with an alphabet or underscore.
  • variables can not share same name with a reserved word in JavaScript.
  • variable names are case sensitive so care should be taken during capitalization.

List of reserved words used in javaScript

The table below shows the list of reserved words used in JavaScript. Please note that this words cannot be used as variable names.

abstract boolean break byte
class char catch case
const continue default debugger
else double do delete
enum export extends false
for float finally final
function goto if implements
interface int instanceof import
long new native throws
protected private package null
public return short static
this synchronized switch super
throw throws transient true
volatile var void typeof
while with in  

CONCLUSION

With the help of this article on variables we hope you now fully understand the meaning and usefulness of variables in JavaScript. lets move on to the next topic on JAVASCRIPT OPERATORS

Leave a Reply

Your email address will not be published. Required fields are marked *