The Variables in the JavaScript Language - JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

Chapter 4. The Variables in the JavaScript Language

The Different Data Types in JavaScript

Similar to other programming languages, JavaScript has its own set of supported data types. Thus, there are certain values and information that you can represent and manipulate using JavaScript.

This programming language supports these basic data types:

· Boolean (i.e. true or false)

· Strings (e.g.“This book is awesome.”)

· Numbers (e.g. 1, 2, 3, 123, etc.)

JavaScript also supports null and undefined, two data types that describe a single value. Lastly, this language uses a complex type of data called“object.” You’ll learn about objects later in this book.

Important Note: JavaScript treats all numbers as floating-point values.

The Variables

As a JavaScript user, you may think of variables as containers: you may place different types of information in a variable. To access the stored data, you just have to specify the container that holds it.

You have to declare variables before you can use them. In JavaScript, you should use var when declaring a variable. Check the following example:

The code given above declares two variables: money and name.

The process of saving data into a variable is known as variable initialization. Most programmers initialize variables during the declaration. However, you may simply declare a variable without saving any value: you can save data into the variable once you need to.

For example, you can create a variable called profitwithout entering any value. Once you have deducted your expenses from your gross income, you may go back to the variable you created and enter the information. To initialize a variable, you just have to indicate the variable’s name, place an equal sign, and enter the value you want to assign. Here’s an example:

var profit;

profit = 1,000,000.00;

JavaScript is considered as an“untyped” computer language. That means JavaScript variables can contain any type of data. This is a great advantage over other languages, which require users to specify the data type during variable creation.

The Scope of a Variable

A variable’s“scope” is the area of your computer program in which it is declared. JavaScript supports the following scopes:

· Local– Local variables are visible only inside the function where they were defined. That means a function won’t be able to use the local variable of another function.

· Global– Global variables are available for all the functions inside the program. That means you can define them anywhere in your codes.

If a local variable and a global variable share the same name, the local variable will take precedence. If you’ll declare a variable locally using the name of an existing global variable, you’ll be hiding that global variable. Analyze the sample code below:

The code given above will show you this result: Local.

Naming Your Variables

While choosing a name for your variables, you should remember the following rules:

· You can’t use any JavaScript keyword (e.g. break, Boolean, etc.) as the name of a variable. You’ll learn about JavaScript keywords later.

· You can use letters, numbers, and an underscore when naming a variable. However, you can’t begin the name using a number. For example, sample1 is valid but 1sampleisn’t.

· The names of variables are case-sensitive. That means JavaScript treats“SAMPLE,” “Sample,” and“sample” as three different variables.

The Reserved Keywords in JavaScript

Similar to other programming languages, JavaScript has a collection of words that serve special purposes. You can’t use these words in naming objects, methods, functions, loops, and variables. The table below will show you all of the keywords in the JavaScript language.

if

do

in

int

new

try

for

var

else

enum

this

long

byte

case

true

null

char

goto

void

with

break

throw

false

final

catch

float

class

const

while

short

super

switch

export

throws

typeof

public

return

delete

static

double

import

Instanceof

abstract

synchronized

Boolean

interface

extends

transient

finally

package

private

protected

function

volatile

continue

debugger

implements