Learn more about JavaScript Variables

Learn more about JavaScript Variables: Variables are an important part of JavaScript.

They are a way to store information in your web browser for a short period of time. You can then use the stored data to build programs that make your web page interactive.

Without variables, your browser has no way of remembering information.

So when we use JavaScript to code a program that will check a password, we need to use a variable to store the data for the correct password.

Without a variable, our browser won’t be able to check if the user has entered a correct password. Variables store data in the form of words or numbers.

You have to code variables in a certain way. You always have to tell your browser you are creating a variable. This is called defining a variable. Let’s have a look at how we create a variable that will store users name:

var userName = "Bob"; 

Let us simplify the above code:

var is the Variable keyword. Tells browser your declaring a variable. userName is the Variable Name. = “Bob”; assigns the Value inside the brackets to the userName variable. In this case assigns Bob text to userName variable.

Variables are always written in the same way. Every piece of JavaScript tells your browser a different piece of information and will be important when you build a program.

A JavaScript variable needs the following:

  • A keyword: To define a variable you have to use the variable (var) keyword. It tells your browser you are creating a variable.
  • A variable name: The next part of a variable is the variable name. You can give your variable any name you like. The only rule with variable names is that they can’t have spaces (so use camelCase instead) and can’t begin with a number.
  • A value: You then have to give your variable a value using the equals sign (=). Doing this is called assigning a value. If the value of your variable is text, you have to put the value in double quotes (” “). You can include spaces in your values. After the value, you have to include a semi-colon (;).

 

Related Links:

Online JavaScript Compiler. Code on the go.

JavaScript Glossary – Variables

Object-Oriented Programming (OOP)

Building a web page with HTML tags

Defining What an Application Is

Comparing Python to other languages

Introduction to JavaScript – Libraries

How do I start a WordPress blog? (hosting wordpress)

MORE CSS PROPERTIES

Helping humans speak to the computer

Learn Code Introspection Python Programming

Introduction to JavaScript – Variables: Review

Introduction to JavaScript – Variables: String Interpolation

Python String Formatting

Redirect New Registered Users to a Specific Page – WordPress

Learn Modules and Packages in Python programming

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.