Top Ad unit 728 × 90

Libreoffice

Libreoffice Writer

Variables in JavaScript

 


Variables in JavaScript

Variables are used for storing data value. JavaScript variables are container (Placeholder)

 for storing value. a variable an have different phases for execution.  And global variable.

 Since the content of a variable varies throughout the execution of a program, they are

 called variable.

 

Declaring JavaScript variables


Creating variable in any programming language is called “declaring” a variable.

 JavaScript variable can be declared with the var keyword. 


There are some rules while declaring a JavaScript variable (also known as identifiers).


1. Name must start with a letter (a to z or A to Z), underscore (_), or dollar ($) sign.


2. After first letter we can use digits (0 to 9), for example value1.


3. JavaScript variables are case sensitive, for example x and X are different variables.

 

Ex1.


In below Example, variable var1 is defined with initial value of 100 where variable

var2 is defined without any initial value.

 

JavaScript Code




Output



Ex2.a,b ,and sum, are variables-


JavaScript Code

 


Output




JavaScript Variable Scope


The scope of a variable is the region of your program in which it is defined. A JavaScript

 variable is simply a name of storage location. JavaScript variables have only two

 scopes.

 

JavaScript local variable


A JavaScript local variable is declared inside block or function. It is accessible within the

 function or block only. For example:


JavaScript Code




Output

 







JavaScript global variable


A JavaScript global variable is accessible from any function. A variable i.e. declared

 outside the function or declared with window object is known as global variable.


JavaScript Code




Output




Declaring JavaScript global variable within function


To declare JavaScript global variables inside function, you need to use window object. 


Automatically Global Variable


When a variable used for the first time inside a function, is assigned a value without declaring (using var).will become global automatically.


JavaScript Code




Output





Local Variables are deleted when function is completed where as global variable are deleted when page execution is completed. Argument is always local.

 

Note:- JavaScript  variable are loosely-typed which means it does not require a data type to be declared like other programming language .you can assign any type of literal values to a variable e.g. string, integer, float, Boolean etc.


JavaScript Code




Output




JavaScript Reserved Words


A list of all the reserved words in JavaScript is given in the following table. They cannot


 be used as JavaScript variables, functions, methods, loop labels, or any object names.

 

abstract

else

instance of

switch

Boolean

enum

int

synchronized

break

export

interface

this

byte

extends

long

throw

case

false

native

throws

catch

final

new

transient

char

finally

null

true

class

float

package

try

const

for

private

typeof

continue

function

protected

var

debugger

goto

public

void

default

if

return

volatile

delete

implements

short

while

double

in

super






Declare multiple Variables in JavaScript

 


In this article, we will see how to declare multiple Variables in JavaScript. The

Variables can be declared using varlet, and const keywords. Variables are

Containers that store some value and they can be of any type.

These are the following ways to declare multiple variables:

 


Table of Content

 

1) Declaring Variables Individually

 

2) Declaring Variables in a Single Line

 

3) Using Restructuring Assignment

 

Data Types in Java Script


Data Type is used to define what type of data can be stored and manipulated with in a 

program. There two types of data type sin JavaScript.

 

Primitive Data Type


String-


It represents sequence of e.g. “AD Computer Campus”.


Number-


It represents numeric values (integer, float, hexadecimal, octal, and exponential


Boolean-


It represents Boolean Value either false or true.


Undefined-


It represents undefined value.


NULL-


It represents null i.e. no value at all.

 

Non- Primitive (or reference) data Type


Object


It represents instance thought witch we can access members. JavaScript object are commas.


Ex.


var person= {firstName;”Manish”,lastName:”Shukla”,age:30,eyecolor:”black”};


Array


It represents group of similar values.java script arrays are written with square brackets. 

Array items are separated by commas.


Ex.


Var student Name= [“Manish”,”Asish”,”Puneet”]                           

 

RegExp


It represents regular expression.


JavaScript Code






Output





Variables in JavaScript Reviewed by ADcomputercampus on April 06, 2024 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.