Variables in JavaScript
In JavaScript, variables are used to store data values. They are declared using keywords like `var`, `let`, or `const`, and can hold various types of data such as numbers, strings, objects, or functions throughout the execution of a program.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Variables</title>
</head>
<body>
<script>
// Variable declaration
var x, y, z; //Assignment
x = 10; //int variable
y = 5.68; //float or double
z = "Hello"; //String
//Print variables
document.write("The value of x is " + x);
document.write("<br>The value of y is " + y + " and the value of z is " + z)
</script>
</body>
</html>
Run Code
Real life example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Variables example</title>
</head>
<body>
<script>
var name = "John Doe";
var place, age;
place = "New York";
age = 25;
document.write("<h3>Hello I am " + name + " from " + place + ", I am " + age + " years old</h3>");
</script>
</body>
</html>
Run Code