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:
To create variable there is syntax you should be familiar with:
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.
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,
Confirm command can use to ask user whether he want to continue on or not. Let's take an
confirm('This will continue to next page,'+'\n'+' Do you want to continue??');
Here how it will look, This 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? :-)
*Types of variables:
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
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.
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 namevar _myVariable is valid. A) True
2.The variable name can start with a [i]'$'
[/i]sign? A) False
3.Variables are of two types Local variables and ______ ?? A) Global
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
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.