Variables a limited lifetime. When we close the page

Variables – Storing Data In MemoryData can be stored either permanently or temporarily.A variable is used to store temporary data that can be altered, or varied.Another bonus of variables is that unlike permanent storage, which might be saved to disk or magnetic tape, variables are held in the computer’s memory. This means that it is much, much faster to store and retrieve the data.Variables have a limited lifetime. When we close the page or move to a new one, our variables are lost, unless we take some steps to save them somewhere.Every variable is given some name so that it can be called whenever needed in the code.But, certain rules should be followed to give name.Variable names are case sensitive. So, myVariable and myvariable are totally different from each other.We can not use certain names and variables for our variable names. Names which can not used are called Reserved words. Certain characters should not be there in variable names. For example the percent sign(%) and the ampersand sign (&) should not be there.Number can be used in variable names but numbers should not be in the beginning of the variable name.Creation of Variables and Value AssignmentBefore we can use a variable, we should declare its existence to the computer using the var keyword. This warns the computer that it needs to reserve some memory for our data to be stored in later. We have to write following line.var myFirstVariable;JavaScript is a weakly typed language; we don’t need to limit us to what type of data a variable can hold.We put data into our variables, a process called assigning values to our variables, by using the equals sign (=).An example of assignment is,myFirstVariable = 101;Example of Declaring a VariableLet us take an example in which a variable is declared , assigned some values and content of that variable is accessed. So the example is given below.Save this program with .html extension and load it in any browser.An alert box will pop in the browser window , which will be like the below diagram.When we will click ‘OK’ in the popup box then the new alert box will pop in the browser window and the new alert box will be like this,Now, its time to understand what is happening inside the script block.Firstly, a variable is declared named as myFirstVariable . After declaring the variable some value is assigned to it.myFirstVariable=”Hello Geeks”;The content inside the variable is showed with the help of alert() function.alert(myFirstVariable);After showing the value of variable , it has been assigned new value by this line of code.myFirstVariable = 12345;Again it is shown with the help of alert() function.alert(myFirstVariable);Data Types in JavaScriptAs we know that data can be of various forms , or types . Javascript is a weakly typed language and a lot more forgiving about how we use different types of data. Let’s take a brief look at some of the more commonly used data types: numerical, text, and Boolean.Numerical DataNumerical data can be in two forms:Integer formFloating point number formBut, JavaScript treats both numerical data as floatingpoint numbers. In javascript the variable is assigned with numerical data like this,var number1 = 34.289;var number2 = 100;We can also use scientific notation to the assignment of the value of variables.StringString simply means text. We can use single or double quotes. We can use quotes inside a string, as long as they don’t match the quotes surrounding the string.var message=”It’s alright”;BooleanThe use of yes or no, positive or negative, and true or false is commonplace in the physical world. The concept of “yes or no” is so useful it has its own data type in JavaScript: the Boolean data type. The Boolean type has two possible values: true for yes and false for no. The purpose of Boolean data in JavaScript is just the same as in the world outside programming: They enable us to answer questions and make decisions based on the answer.var boolean1 = true;A variable type can be changed easily .var dumb = “Bye Geeks”;dumb = 123;Here , in this example type of variable dumb is changing from text to numerical data.

x

Hi!
I'm Neil!

Would you like to get a custom essay? How about receiving a customized one?

Check it out