Variables and data types in JavaScript
by danielkwood
HTML
<html>
<head>
<title>Variables</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
JavaScript
// Data types in JavaScript include:
// String - characters (letters, numbers, or symbols)
// Integer - whole numbers
// Float - floating point (decimal) numbers
// Boolean - true/false only
// These are examples of string variables:
var user = "Joe";
var greeting = "Hello there";
// This is an integer variable:
var score = 50;
// This is a float variable:
var average = 20.57;
// This is a boolean variable:
var finishedGame = false;
// Example of joining (concatenating) variables and strings together to display on screen
// You can use , or + to join variables or strings together
document.write(greeting + ", " + user + ". Your score is " + score , ". The average score is " + average);
// You can include HTML tags in string values:
document.write("<h1>Welcome, " + user + ".</h1>");
// Make sure you escape quotes within a string using the \ backslash character
document.write("Joe said \"hello\".");