[go: up one dir, main page]

0% found this document useful (0 votes)
4 views6 pages

Variables and Data Types

JavaScript variables are containers for storing data values and must be declared using the 'var' keyword. Variables can hold different data types and have two scopes: global and local. JavaScript also includes primitive data types such as numbers, strings, booleans, undefined, and null, as well as composite data types like objects.

Uploaded by

vparshant839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Variables and Data Types

JavaScript variables are containers for storing data values and must be declared using the 'var' keyword. Variables can hold different data types and have two scopes: global and local. JavaScript also includes primitive data types such as numbers, strings, booleans, undefined, and null, as well as composite data types like objects.

Uploaded by

vparshant839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Trade: COPA UNIT:JavaScript

Topic:JavaScript - Variables
JavaScript Variables
JavaScript variables are containers for storing data values. Variables can be thought of as named
containers.we can place data into these containers and then refer to the data simply by naming the
container.
Before we use a variable in a JavaScript program, we must declare it. Variables are declared
with the var keyword as follows.
<script type = "javascript">

var money;
var name;

</script>

Example 1: In example 1, a, b, and c, are variables:

var a = 12;
var b = 10;
var c = a + b;

From example 1, we can understand that


• a stores the value 12
• b stores the value 10
• c stores the value 22
Example 2: In example 2, mark1, mark2, and total, are variables:

var mark1 = 85;


var mark2 = 66;
var total = marks1 + mark2;

we can also declare multiple variables with the same var keyword as follows −
<script type = "text/javascript">

var money, name;

</script>

 Variable Initialization:Storing a value in a variable is called variable initialization.we can


do variable initialization at the time of variable creation or at a later point in time when we need
that variable.
Example:
<script type = "text/javascript">

var name = "Ali";

1
var money;
money = 2000.50;

</script>

[Note − Use the var keyword only for declaration or initialization, once for the
life of any variable name in a document. we should not re-declare same variable
twice.
JavaScript is untyped language. This means that a JavaScript variable can
hold a value of any data type. Unlike many other languages, we don't have to tell
JavaScript during variable declaration what type of value the variable will hold.
The value type of a variable can change during the execution of a program and
JavaScript takes care of it automatically.]
JavaScript Identifiers:All JavaScript variables must be identified with unique
names. These unique names are called identifiers. Identifiers can be short names like a
and b or more descriptive names like mark1, mark2, total, age, sum, total volume.
The general rules for constructing names for variables are:
• Names can contain letters, digits, underscores, and dollar signs.
• Names must begin with a letter
• Names can also begin with $ and _
• Names are case sensitive (a and A are different variables)
• Reserved words like JavaScript keywords cannot be used as names

. For example, 123test is an invalid variable name but _123test is a valid one.

[Note: JavaScript identifiers are case-sensitive.]

Case Sensitivity:JavaScript is a case-sensitive language. This means that the language


keywords, variables, function names, and any other identifiers must always be typed with a
consistent capitalization of letters.
So the identifiers Time and TIME will convey different meanings in JavaScript.

NOTE − Care should be taken while writing variable and function names in JavaScript.

JavaScript Variable Scope


The scope of a variable is the region of our program in which it is defined. JavaScript variables
have only two scopes.
 Global Variables − A global variable has global scope which means it can be defined
anywhere in our JavaScript code.
 Local Variables − A local variable will be visible only within a function where it is
defined. Function parameters are always local to that function.
Within the body of a function, a local variable takes precedence over a global variable with the
same name. If we declare a local variable or function parameter with the same name as a global
variable, we effectively hide the global variable. Take a look into the following example.
<html>

2
<body onload = checkscope();>
<script type = "text/javascript">

var myVar = "global"; // Declare a global variable


function checkscope( )
{
var myVar = "local"; // Declare a local variable
document.write(myVar);
}

</script>
</body>
</html>

This produces the following result −


local

JavaScript Reserved Words


A list of all the reserved words in JavaScript are given in the following table. They cannot be
used as JavaScript variables, functions, methods, loop labels, or any object names.

abstract else instanceof 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

3
default if return volatile

delete implements short while

do import static with

double in super

JavaScript Datatypes
One of the most fundamental characteristics of a programming language is the set of data types
it supports. These are the type of values that can be represented and manipulated in a
programming language.
Primitive Data:A primitive data value is a single simple data value with no additional
properties and
JavaScript allows us to work with three primitive data types −
1. Numbers(Primitive) JavaScript has only one type of numbers. Numbers canbe
written with or without decimals. , eg. 123, 120.50 etc. Numbers are written without
quotes.
Example: var side = 10; // Number
var num1 = 87.0; // Written with decimals
var num2 = 87; // Written without decimals

Extra large or extra small numbers can be written with scientific (exponential)
notation:

Example 19
var exp1 = 232e5; // result is 23200000
var z = 123e-5; // result is 0.00232

2. Strings(Primitive) :A string or a text string is a series of characters like “Harini


Kumar”. Strings are written with quotes.. Strings are written inside double or single
quotes. e.g. "This text string" etc

Example: var firstName = “Rithika”; // String


Note1: If we put a number in quotes, it will be treated as a text string.
Note2: If you put a number in quotes, the rest of the numbers will
be treated as strings, and concatenated.
Example:
var x = “7” + 2 + 5;
The result will be 725.

3. Boolean(Primitive) : Booleans are often used in conditional testing

4
e.g. true or false.
4. Undefined: In computer programs, variables are often declared without a value. The
value can be something that has to be calculated, or something that will be provided
later, like user input. A variable declared without a value will have the value undefined.
In JavaScript, a variable without a value, has the value undefined. The typeof is also
undefined.
Example 24
var bike; // Value is undefined, type is undefined
[Note : Any variable can be emptied, by setting the value to undefined. The type
will also be
undefined.]
Empty Values: An empty value has nothing to do with undefined. An empty string has
both a legal value and a type.

Example 25
var bike = “” ; // The value is “”, the typeof is “string”

Null:In JavaScript null is “nothing”. It is supposed to be something that doesn’t exist. In


JavaScript, the data type of null is an object. we can empty an object by setting it to
null.
Example:
var personName = {firstName:”Harini”,last Name; ”Kumar”, age:13, height:”155 cms”};
personName = null; //Now value in null, but type is still an object

we can also empty an object by setting it to undefined

Example:
var personName = {firstName:”Harini”, lastName: ”Kumar”, age:13,
height:”155 cms”};
personName = undefined; // Now both value and type is undefined.

5.Object(Composite Data Type): JavaScript objects are written with curly braces.
Object properties are written as name:value pairs, separated by commas.
Example: var x = {firstName:”Harini”, lastName:”Kumar”}; // Object
Example:
var personName = {firstName:”Harini”,lastName: ”Kumar”,
age:13,height.”155 cms”};

The object (personName) in the example 22 above has 4 properties:


firstName, lastName, age and height.

Summary:
1.JavaScript also defines two trivial data types, null and undefined, each of which defines only
a single value.
2. In addition to these primitive data types, JavaScript supports a composite data type known
as object.

Note − JavaScript does not make a distinction between 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.

Dynamic data types: JavaScript has dynamic types. This means that the samevariable can
be used to hold different data types:

5
Example 15
var z; // Now z is undefined
z = 10; // Now z is a Number
z = “Sakthi”; // Now z is a String

Difference Between Undefined and Null


Undefined and null are equal in value but different in type.
Example:
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true

You might also like