Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Variable in JavaScript

Explore Variable in JavaScript with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers variable in JavaScript with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

JavaScript Data Type

JavaScript lets you work with the following three types of original data:

  • Numbers, for example : 123, 120. 50, .
  • Text string , for example: "This text string", .
  • Boolean For example: true or false.

JavaScript also defines two common data types, Null And Undefined , each of which only defines a single value. In addition to these data types, JavaScript supports a mixed data type called Object . We will discuss the details of objects in a separate chapter.

Note - Java does not distinguish integer values and floating-point values. All numbers in JavaScript are represented as floating point values. JavaScript represents numbers using the 64 bit floating-point format defined by the IEEE 754 standard.

Variables in JavaScript

Like many other program languages, JavaScript has variables. Variables can be considered a named container. You can put data into this container and then refer to this data simply by naming the container.

Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the keyword Var As follows:

 type = "text/javascript" > 

You can also declare multiple variables with the same Var Keyword as follows:

 type = "text/javascript" > 

Retain a value in a variable called Initialization variable . You can initialize the variable at the time of the variable creation or at the time after you need that variable.

For example, you can create a variable named money and assign the value 2000. 50 to it later. With another variable, you can assign a value at the time of initialization as follows:

 type = "text/javascript" > 

Note - Only use the Var Keyword for declaration or initialize variables, once for any variable name in the document. You should not re-declare the same variable twice.

JavaScript is Untyped Language (not typed). This means that a JavaScript variable can hold a value of any data type. Unlike many other languages, you don't have to tell JavaScript during the variable declaration process about the type of value that variable holds. The value type of the variable can change during the execution of a program and JavaScript takes care of it automatically.

Variable Scope in JavaScript

The scope of a variable is the program area in which it is defined. JavaScript variables only have the following two ranges:

Global variable - A Global variable has a common scope, meaning that it can be defined anywhere in JavaScript code.

Local variable - A Local variable will only be visible in a function, where it is defined. Function parameters are always internal to that function.

In the body of a function, a Local variable has a higher priority than the Global variable with the same name. If you declare a Local variable or a function parameter with the same name as the Global variable, you hide that Global variable effectively. See the following example:

 type = "text/javascript" > 

It will produce the following result:

 local 

Variable Names in JavaScript

While naming variables in JavaScript, you should remember the following rules:

You should not use any reserved keywords for a variable name. These keywords are mentioned in the next section. For example, variable names Break or Boolean Are invalid.

JavaScript variable names should not start with numbers (0-9). They must start with a letter or underscore character. For example, 123test Is an invalid variable name but _123test Is valid.

JavaScript variable names are case sensitive. For example, Name And Name Are two different variables.

Dedicated Keywords in JavaScript

The following table lists the list of reserved keywords in JavaScript. They cannot be used as variables, functions, methods, loop labels, or any other object names in JavaScript.

Abstract

Boolean

Break

Bytes

Case

Catch

Char

Class

Const

Tiep tuc

Debugger

Default

Delete

by

Double

Else

Enum

Export

Extends

False

Final

To finish,

Float

Cho

Jaw

Goto

if

Implements

Import

Print

Instanceof

Int

Interface

Long

Native

New

Null

Package

Private

Protected

Public

Return

Short

Static

Super

Switch

Synchronized

This

Throw

Throws

Transient

True

Try

Typeof

Var

Void

Volatile

While

With

Follow tutorialspoint

Previous post: JavaScript location in HTML File

Next lesson: Operator in JavaScript

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.