JavaScript is one of the most popular programming languages for the Web (it is said 95.2% of all websites use JavaScript). JavaScript can change and update both HTML and CSS in addition to a whole host of other features.
JavaScript (or JS from here on) uses many data types, below is some of the most important.
JavaScript Variables
Variables are essentially containers for storing data values.
Below we use the variables are a, b, c
var a=10;
var b=5;
var c=a + b;
What you can expect is:
- a stores the value of 10
- b stores the value of 5
- c stores the value of 15 (a + b)
JavaScript Strings
Strings store text, the text within these should not be confused with other variable types (numbers, etc.). You can use single or double-quotes.
1
2
|
var bike = "Canyon" ; var bike = 'Canyon' ;
|
If you are looking to determine the length of a string, you can use JavaScripts built-in property length.
1
2
3
4
|
var bike = "Canyon" ;
var bikeln = bike.length;
Result: 6
|
JavaScript Objects
Objects are variables, but, they can contain many values within one variable.
Below we assign a simple value (Aeroad) to our variable named bike:
Below we assign many values (Aeroad, stealth, large) to our variable named bike:
1
|
var bike = {model: "Aeroad" , color: "stealth" , size: "large" };
|
JavaScript Arrays
Arrays are used to store multiple values in a single variable.
1
|
var bike = ["Canyon", "Trek", "BMC"];
|
JavaScript Functions
A function is a block of code that can be called (invokes) from another function or within the code.
It is designed to perform a set task and normally will return something.