Skip to main content

JavaScript Variables-Storage & Declaration


Now we have understood how simple scripting in JavaScript is written.

Let us take in some input from the user using the prompt(); function.
  • prompt(); function:
The prompt function prompts the user to input data into it by providing a textbox.You also get two buttons to input your reaction.Hence the interactivity.

Now the website has asked the input data, now let it process it and act upon it.

You have entered the data asked by the webpage prompt, now let us store it in a variable.

STORING OF VARIABLES USING prompt();
Storing the data directly upon entering is done using the variable declaration along with the prompt input function separated by a "=" sign.
var num = prompt("Enter a number");

DECLARING OF VARIABLES
In JavaScript variables are declared using the "var" keyword. Any variable can be created using this keyword, irrespective of the type of data it stores viz.textual,numerical,symbolical,patterns.

Sample variable declaration
var num;
var name;
var pattern;

The above three declarations can be further assigned specific values by the user.
Sample variable assignment
num=10;
name="JavaScript"
pattern="/[1-9]/;
  1. The first assignment assigns a numerical value of 10 to the variable num.
  2. The second assignment assigns a textual value of Javascript to the varialble name.
  3. The third assignment is a bit peculiar in nature, as the name of the variable suggests it is a pattern, it can intelligently identify the digits between 0 to 9 including 0 and 9 both.
    In general it is called a 'REGULAR EXPRESSIONS'.
    Using such regular expressions one can design their own intelligent variables.
    Let us revisit this in more depth in a while after we visit functions.

    I would suggest you to have a look at what Regular Expressions are here before we approach them, as it would be helpful to have a more intelligent and creative session with them.

    The sample program below showcases the declaration and storage of variables.



1:  <?xml version = "1.0" encoding="utf-8"?>   
2:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   
3:  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   
4:  <html xmlns = "http://www.w3.org/1999/xhtml">   
5:  <head>  
6:  <title>JavaScript </title>  
7:  </head>  
8:  <body>  
9:  <script>  
10:  var num = prompt("Enter a numbe r");  
11:  var name = "JavaScript";  
12:  alert("The number you entered now was" +num);  
13:  document.write("I am "+name);  
14:  </script>  
15:  </body>  
16:  </html>  

Comments

Popular posts from this blog

WebGL

The WebGL is a part of the JavaScript language library. It enables one to create interactive interfaces and creative environments. The latest example that we come across is the Sony PlayStation 4 (PS4) . The whole startup environment of the menu and all the tiles are made in WebGL exclusively. It is an extremely powerful web technology library, which makes UI design using the 3D models and interfaces very fluid and simple. It provides an elegant look to interfaces. It runs on the OpenGL 2.0 model. All the web browsers are usually equipped to handle this version of the OpenGL but the higher versions of OpenGL requires professional graphics cards like Nvidia's Professional Graphics soultions as Nvidia Quadro Series. NVIDIA Quadro K2000D Nvidia Tesla Series And the AMD's professional graphics solution are AMD FirePro Series. ATI FirePro V5900 ATI FirePro V7900  Now once the content is created we need to test the results as they are to be sol

Games-Fun for all

Game is an activity generally dealt with at leisure times and for amusement. We have been carrying out this activity from it's inception, since our childhood and the childhood of the information era. Games were more prominently played in the real world between people in real time without any artificial intelligence, but after the information age we started to play with the artificial intelligent systems. Let us have a look how the games have modified themselves or they have not. Games were "fun for all" when they were played without artificial intelligence. The digital information cannot remain still, it requires a steady flow as the water takes away the yatch on a wonderful and pleasant journey though intelligently handed mast by a sailor navigating it's way to it's destination. It requires both flow and navigation, i.e. interface and content. Earlier the television and the hardware encapsulated the null void , now it is slowly getting encapsulated by th

SMITE Open Beta Review

The developers of Global Agenda and Tribes: Ascend have yet another major offering to keep you glued to your screens, now providing an opportunity to play 'Mythological Godlike avatars' in their latest offering called Smite. Placing yourself in the shoes of the 'Almighty' or a bunch of them 'All Mighty' if I might say, itself should give you an idea of what herculean tasks you would be asked to handle. The feeling of 'omnipotence' brings an exceptional nature to this game. Limitless flow of power is just a part of the experience, but having an ultimate command over it and other such myriad abilities can seal the deal for you. Spell slingers, knights in shining armor, snoopy hunters, shady assassins, brute warriors are the new God lineages here. Outgrowing heightened expectations is the least things you should expect as this mythological battlefield resides over the whole wide internet. Teleporting, Flying, Sprinting, and digging, you will have