Variable in JavaScript

One of the most distinctive features of a program language is the set of data types it supports. This is the type of value that can be represented and manipulated in the program language.

JavaScript data type

One of the most distinctive features of a program language is the set of data types it supports. This is the type of value that can be represented and manipulated in the program language.

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

  1. Numbers, for example : 123, 120.50, .
  2. Text string , for example: "This text string", .
  3. 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

tiếp tục

debugger

default

delete

by

double

else

enum

export

extends

false

final

finally

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

5 ★ | 1 Vote