Visit: https://financeclub.viden.io
JavaScript has dynamic types. This means that the same variable can be used to hold different data types:
var x; // Now x is undefined
x = 5; // Now x is a Number
x = "John"; // Now x is a String
A string (or a text string) is a series of characters like "John Doe".
Strings are written with quotes. You can use single or double quotes:
var carName1 = "Volvo XC60"; // Using double quotes
var carName2 = 'Volvo XC60'; // Using single quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
var answer1 = "It's alright"; // Single quote inside double quotes
var answer2 = "He is called 'Johnny'"; // Single quotes inside double quotes
var answer3 = 'He is called "Johnny"'; // Double quotes inside single quotes
You will learn more about strings later in this tutorial.
JavaScript has only one type of numbers.
Numbers can be written with, or without decimals:
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
Extra large or extra small numbers can be written with scientific (exponential) notation:
var y = 123e5; // 12300000
var z = 123e-5; // 0.00123
You will learn more about numbers later in this tutorial.
Booleans can only have two values: true
or false
.
var x = 5;
var y = 5;
var z = 6;
(x == y) // Returns true
(x == z) // Returns false
Booleans are often used in conditional testing.
You will learn more about conditional testing later in this tutorial.
JavaScript arrays are written with square brackets.
Array items are separated by commas.
The following code declares (creates) an array called cars
, containing three items (car names):
var cars = ["Saab", "Volvo", "BMW"];