<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Understanding JavaScript part - 2 (Introdution to variables and pop-up boxes)

Published on
9,722 Points
3,522 Views
2 Endorsements
Last Modified:
Approved
Hello,
It's been long time since I wrote my last article on this series. You may like to read first part of this series named Understanding-javasript-part-1-getting-started. Let's continue on with part 2. Today you will learn how to declare/create variables, how to use them, what are their types, and what are pop-up boxes.

*Introduction to Variables:

What are variables? Variables are nothing but a construct in which we store values. Variables are important in Javascript. You should get familiar with this term. Don't worry,by the end of this topic you will be more familiar with variables.

Creating Variables:

To create variable there is syntax you should be familiar with:

Syntax:
var your_variable_name = "string" or (number);

Open in new window

Notes,
Variables are declared by 'var' keyword. It is like a some set of characters which help browser to recognized that you are declaring variable.
After 'var' keyword you can declare name of your variable. Variables name can be anything you like,but make sure you are declaring a name which will further help you to recognized easily what you stored in that particular variable
After that values must be assign to variables. Use '=' assignment operator to assign values to variable.
You can also declare variable first and then assign value to it later in a script.
You can assign anything to variable like numbers ,strings,any data etc.
*Strings: Strings are words or character which you enters.
*Numbers: 14234536 anything
*Data: You can also assign a anchor,image or another tag to variable.
And lastly use semicolon (;) to finished declaring variable.
Example
var (then) my_first_variable = 'this is var';

Open in new window

That's it! You just created your first variable.So lets checked if you do it right or so,
To check what is variable use 'alert' command to check this out.

alert(my_first_variable);

Open in new window


You will get whatever contain you defined variable in output screen.In this case you will get 'this is var'

*Pop-up boxes

Before we go further lets take a look at what are pop-up boxes,

Like alert which you know very well now and which use to show alert messages to user we have two more commands available which works very much similar to alert.

Prompt
Confirm.

1. Prompt

In prompt you can ask questions to user and without answering them he could not go further in you web site. This prompt boxes are useful to get user's name or city name etc.

Example
prompt('Please enter your name','xyz')

Open in new window


Here in above code,first string, i.e. in between quotes ('') will ask a user his name and second string will give some default value to it so that user can change it or leave it as it is. However second one is optional.

The output of above will be,

Prompt Box

2. Confirm

Confirm command can use to ask user whether he want to continue on or not. Let's take an example,

confirm('This will continue to next page,'+'\n'+' Do you want to continue??');

Open in new window


Then user will get two options ('OK','cancel') then you can use Javascript to execute bit of code if user clicks on 'OK' or execute another bit of code if he clicks 'cancel' etc.
Here how it will look,
Confirm boxThis above three commands commonly known as 'pop-up' boxes. Pop-up boxes normally use in demonstration purpose only. You should be careful while using this boxes on your remote website at a time this boxes may become frustrating to your users and they might leave your website,and that what you don't want at any cost. right? :-)

*Continue on with variables:

var myElement = 'This is My variable';

Open in new window

As you know variable can have stings or numbers in it. Like other programming languages,there is one more advantage in Javascript that there is no float, int like keywords to define strings and numbers separately. In Javascript we only use 'var' keyword to define all this things.

*Types of variables:

1. Global

2. Local


Global Variables:
The defined Global variables are available to use anywhere in Javascript. The scope of global variables is far more larger than that of local variables. Declaring global variables are easy,

<script type="text/Javascript">
	Var my_Global_Variable = "This is my global variable which i can use anywhere in Javascript"
</script> 

Open in new window


Local Variables:
Local variables are those who can not used anywhere in Javascript,they have some limitation. Local variables are declared in function. Their scope is small but they are far more important than global ones. You should always avoid to make global variables.

<script type="text/Javascript">
	Function myfuncion() {
		Var my_local_Variable = "this variable is only used in this particular function"
	}
<script>

Open in new window


In simple words,
Variables which are outside of functions are global variables and those who are inside functions are local variables.
<script type="text/Javascript">

Var global_variable = "This is global variable";

Function myfunction () {
	Var myElement= "This is local variable";

}
</script>

Open in new window


*Something you should keep remember:

Variables must be start with keyword 'var'.
Variable can have any name you like such as myelement, myshop etc.
Remember that variables name must not start with,
*Numbers,
*Special characters like (%,^,&,!,#, etc),
Variable name can start with any alphabet and special characters like _ $ .
Variables can have strings and numbers in it
Variables must have semicolon(;) at end of it
Variables can call at any time any where in Javascript if they are global.
local variable is only valid inside of one specific function only where they are declared.
Whenever possible declared local variables only. Always try to avoid declaring global variables.

*Summary:

By the end of this article you have got basic knowledge of declaring and creating variables. You also knew how to create pop-up boxes on your own,how they works and when to use them. So try practicing them and get grip of this topic.Next up,statements in Javascript.

Quiz,


Lets quickly take a quiz on variables and check if oyu can solve this question.The answers will be at end of this article.I will suggest you not to check answers without solving it first.

1.The variable name
var _myVariable is valid.
A) True
B) False.

2.The variable name can start with a
[i]'$' [/i]sign?
A) False
B) True

3.Variables are of two types Local variables and ______ ??
A) Global
B) Nobal

4.What is the purpose of prompt command??
A) To get a name from user and display it in webpage
B) Ask user whether s/he want to continue on further.

5.The syntax of variable is,
A)int your_variable_name = "string" or (number)
B)var your_variable_name = "string" or (number);

Answers:
1: B
2: B
3: A
4: A
5: B

Hope everybody have got full marks. If not then please go on and read this article again and specially grab variables concept perfectly. See you at next part.
2
Comment
Author:d4durvesh
0 Comments

Featured Post

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Join & Write a Comment

The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month