Try the Javascript Course for Free!

JavaScript Variables- Tutorial

/ / JavaScript, Latest

JavaScript Variables: Video Lessons

            The above video lessons, titled “What are JavaScript Variables?,” “Syntax for Text and Numerical Values,” and “Creating (Declaring) Variables,” help you learn about JavaScript variables and how to declare JavaScript variables. These lessons are from our complete Introductory JavaScript tutorial, titled “Mastering Introductory JavaScript Made Easy v.1.0.”

JavaScript Variables: Overview

            Much like algebra, JavaScript uses letters as containers for storing information, for example: var x=7;. In the previous example, the letter “x” is assigned a numerical value of 7, declaring that any variables with the letter “x” will have a value of 7.

            JavaScript variables can hold expressions as well as single values. This allows JavaScript to be able to perform arithmetic by using operators like = and +, for example: var c=a+b;.

            JavaScript variables can have single letter names, like x or descriptive names, like sum or totalvolume. It is important to note the JavaScript variables must start with a letter. Just like JavaScript statements, variables are case sensitive, where y and Y are not the same.

JavaScript Variables: Syntax

            JavaScript variables can have either a numerical or textual value. The syntax for each variable is different. When assigning a numerical value to a variable you only need to input the number: var a=7;.

            When assigning a textual value to a variable you must surround your value with either single or double quotation marks: var a=”Yes”;. A textual value for a variable is considered a “string”. If you place quotation marks around a numerical value it will be treated as text and will not function properly if there is a later mathematical function in the same statement that calls on that variable.

JavaScript Variables: Declaring JavaScript Variables

            When creating JavaScript variables, you “declare” a variable by giving it a value. JavaScript variables are declared using the keyword var. For ease of reading within your code, you can declare a variable and, on the next line, assign a value to that variable. You can also assign a value on the same line as you declare a variable. This allows for smaller file size and faster loading times. It is good practice to declare all your JavaScript variables at the beginning of your code. Doing so makes for easier editing and debugging. For example, the following lines of code will have the same end result:

var person;

person=”John Smith”;

Or

var person=”John Smith”;

JavaScript Variables- Tutorial: A picture from the lesson "Creating (Declaring) Variables" within the "Mastering Introductory JavaScript Made Easy v.1.0" tutorial.

JavaScript Variables- Tutorial: A picture from the lesson “Creating (Declaring) Variables” within the “Mastering Introductory JavaScript Made Easy v.1.0” tutorial.
TOP