Variable in JavaScript

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

May be interested

  • Top site with many good JavaScript exercises to practiceTop site with many good JavaScript exercises to practice
    many people, after taking javascript courses and being equipped with some knowledge of the language they pursue, are eager to improve and cultivate these learned skills. so this article will give you a list of the top 3 websites to actually make javascript.
  • What is JavaScript?What is JavaScript?
    javascript is a programming language of html and web. it is lightweight and most commonly used as part of web pages, but their implementation allows client-side scripts to interact with users and create dynamic websites. it is an interpreted programming language with object-oriented capabilities.
  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • Reference in C ++Reference in C ++
    a reference variable is an alias, which is another name for an existing variable. when a reference is initialized with a variable, then either the variable name or the reference name can be used to reference that variable.
  • Learn about ES6 in JavascriptLearn about ES6 in Javascript
    es6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
  • Declare variables in SQL ServerDeclare variables in SQL Server
    sql server fully exists the concepts of data types, variables and declarations of variables as other programming languages. the article will learn how to declare a variable, multiple variables and assign default values ​​to variables in sql server.
  • Switch Case command in JavaScriptSwitch Case command in JavaScript
    you can use multiple if ... else ... if statements as in the previous chapter to make a multiple choice branch. however, it is always not the best solution, especially when all branches depend on the value of a single variable.
  • Summary of JavaScript exercises with sample codeSummary of JavaScript exercises with sample code
    in order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
  • JavaScript location in HTML FileJavaScript location in HTML File
    there is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.
  • How to Turn on JavaScriptHow to Turn on JavaScript
    javascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...