JavaScript Variables and  Strings

JavaScript Variables and Strings

In this article you’ll learn three tools for storing data in JavaScript: variables, which store numbers or text; strings, which are special variables for working with text; and arrays, which are multiple variables you can refer to by number.

Choosing Variable Names

Variables are named containers that can store data (for example, a number, a text string, or an object). each variable has a name. There are specific rules you must follow when choosing a variable name:

  • Variable names can include letters of the alphabet, both upper - and lowercase. They can also include the digits 0–9 and the underscore (_) character.

  • Variable names cannot include spaces or any other punctuation characters.

  • Variable names are case sensitive TotalNum are separate variable names.

  • There is no official limit on the length of variable names, but they must fit within one line.

Using these rules, the following are examples of valid variable names:

- total_number_of_fish
- LastInvoiceNumber
- temp1
- a
- _var39

Using Local and Global Variables

JavaScript includes the var keyword, which can be used to declare a variable. You can omit var in many cases; the variable is still declared the first time you assign a value to it. to understand where to declare a variable, you will need to understand the concept of scope. A variable’s scope is the area of the script in which that variable can be used. There are two types of variables:

  • Global variables have the entire script (and other scripts in the same HTML document) as their scope. They can be used anywhere, even within functions.

  • Local variables have a single function as their scope. They can be used only within the function they are created in.

To create a global variable, you declare it in the main script, outside any functions. You can use the var keyword to declare the variable, as in this example:

students = 30;

Before you get in the habit of omitting the var keyword, be sure you understand exactly when it’s required. It’s actually a good idea to always use the var keyword—you’ll avoid errors and make your script easier to read, and it won’t usually cause any trouble.

A local variable belongs to a particular function. Any variable you declare with the var keyword in a function is a local variable. Additionally, the variables in the function’s parameter list are always local variables. To create a local variable within a function, you must use the let keyword.

Assigning Values to Variables

“Understanding JavaScript,” you can use the equal sign to assign a value to a variable. For example, this statement assigns the value 40 to the variable lines :

lines = 40;

You can use any expression to the right of the equal sign, including other variables. You have used this syntax earlier to add one to a variable:

lines = lines + 1;

Because incrementing or decrementing variables is quite common, JavaScript includes two types of shorthand for this syntax. The first is the += operator, which enables you to create the following shorter version of the preceding example:

lines += 1;
// Add  one number to the  lines variable

Similarly, you can subtract a number from a variable using the -= operator:

lines -= 1;  
// subtract one number from lines variable

If you still think that’s too much to type, JavaScript also includes the increment and decrement operators, ++ and -- . This statement adds one to the value of lines :

lines++;
// Add one to the value

Expressions and Operators

An expression is a combination of variables and values that the JavaScript interpreter can evaluate to a single value. The characters that are used to combine these values, such as + and / , are called operators.

You can control precedence by using parentheses. Here’s the working statement to calculate an average

result = (a + b + c + d) / 4;
// Calculating average number

The parentheses () ensure that the four variables are added first, and then the sum is divided by four.

Data Types in JavaScript

  • Numbers, such as 3 , 25 , or 1.4142138 —JavaScript supports both integers and floating-point numbers.
  • Boolean, or logical values—These can have one of two values: true or false . These are useful for indicating whether a certain condition is true.
  • Strings, such as “I am a jelly doughnut” —These consist of one or more characters of text. (Strictly speaking, these are String objects, which you’ll learn about later in this chapter.)
  • The null value, represented by the keyword null —This is the value of an undefined variable. For example, the statement document.

    JavaScript keeps track of the data type currently stored in each variable, it doesn’t restrict you from changing types midstream. For example, suppose you declared a variable by assigning it a value: total = 31;