JavaScript Variables

JavaScript Variables

A variable is basically a container or place holder for storing data values.

Variables are fundamental to all programming languages. Variables are used to store data, like a string of text, numbers, etc. The data or value stored in the variables can be set, updated, and retrieved whenever needed. In general, variables are symbolic names for values.

 Declare JavaScript Variables

You can create/ declare a variable using the  var  keyword
Like this

var myName;
var age;
var job;

In this example,  myName, age and job are variables.
So far we have created/declared variables.
But these are just names, they don’t have any data or value.

The Assignment Operator

In JavaScript, the equal sign (=) is called an assignment operator. The assignment just means ‘set a value ‘ to a variable.
Let’s use the variables we’ve created above.

var myName = 'John';
var age = 18;
var job = 'Waiter';

JavaScript Data Types

JavaScript can handle many types of data, but for now, just think of numbers and strings.
JavaScript variables can hold numbers like 100, 18, 25, 7000
and text values like “John Doe”, ‘Jane Doe ‘.

In programming, text values are called strings.
Strings are written inside single or double-quotes. Numbers are written without quotes.
If you put a number inside a quote it will be treated as a string, not a number.
The difference is on numbers computers can do calculations on strings or text they can’t.

var pi = 3.14;
var person = "Lia Sue";
var question = 'Who are you?';

Naming Javascript Variables

These are the following rules for naming a JavaScript variable:

  • Names must begin with a letter.
  • Names can contain letters, digits, underscores, and dollar signs.
  • Names can also begin with $ and _
  • Names are case sensitive (z and Z or x and X are different variables)
  • A variable name cannot contain spaces.
  • A variable name cannot start with a number.
  • A variable name cannot be a JavaScript keyword or a reserved keywords.

Comments are closed.

Let's make it official :) Subscribe to my Youtube Channel.