Variable
A JavaScript variable is simply a name of storage location. There are two types of variables in JavaScript : local variable and global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers).
- Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
- After first letter we can use digits (0 to 9), for example value1.
- JavaScript variables are case sensitive, for example x and X are different variables.
Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows.
Syntax
<script>var name;
var x;
</script>
Data Types in Javascript
javaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.
1) Primitive data type
2) Non-primitive (reference) data type
JavaScript is a dynamic type language, means you don't need to specify type of the variable because it is dynamically used by JavaScript engine. You need to use var here to specify the data type. It can hold any type of values such as numbers, strings etc.
Primitive data type
- String = represents sequence of characters e.g. "hello"
- Number = represents numeric values e.g. 100
- Boolean = represents boolean value either false or true
- Undefined = represents undefined value
- Null = represents null i.e. no value at all
Non-primitive data type
- Object = represents instance through which we can access members
- Array = represents group of similar values
- RegExp = represents regular expression
Example
<html><head>
<title>javascript</title>
<script>
var a;
var name="Riya";
a=10;
document.write("a=",a);
document.write("name=",name);
</script>
</head>
<body>
</body>
</html>
Output
Built-in-function
A function is a piece of code that performs some operation to fullfill a specific task.
function | Description |
---|---|
alert() | Display a dialog box with some information and ok button |
confirm() | Display a dialog box with some information and ok and cancel button |
ParseInt() | Converts a string value into a numeric value |
parseFloat() | Converts a string value into a numeric value and decimal value |
prompt() | Display a dialog box that accepts an input value through a text box. |
alert function
<html><head>
<title>javascript</title>
<script>
alert("Hi, i am alert function");
</script>
</head>
<body>
</body>
</html>
confirm function
<html><head>
<title>javascript</title>
<script>
confirm(" HI,I am confirm function with ok and cancel button");
</script>
</head>
<body>
</body>
</html>
prompt function
<html><head>
<title>javascript</title>
<script>
var name;
name=prompt("Enter your name");
alert(name); </script>
</head>
<body>
</body>
</html>
prompt with parseInt function
if you want to insert integer or float value with prompt function use parseInt or parseFloat function
<html><head>
<title>javascript</title>
<script>
var a;
a=parseInt(prompt("Enter a no"));
alert(a); </script>
</head>
<body>
</body>
</html>