Let's talk about variables.
If you remember math, we had x = 5 y=6 z=x+y Javascript has the same concept. The pattern is the same across the all computer languages. Boolean - true or false Number - any integer String - It could be 1 letter or multiple words. When you set your Strings, make sure you either use double quotes or single. Null - one value, null Undefined - a type that has no value but undefined var is still assigned. Here is the thing. In Javascript, you can get away with not using var when you declare your variable. HOWEVER, I am totally against this practice. If you use the same name of the variable inside of multiple functions, JS will be looking for the "var". You can test out the vars through console.log. Open Console in Chrome; Click on Console; Console.log(typeof "Nike!"); Console.log(typeof 10); Console.log(typeof 3.1); Console.log(typeof true); Console.log(typeof undefined); See what happens.... |
ExamplesEXAMPLES:
// STRINGS VAR NAME = "I THINK THEREFORE I AM"; VAR LETTER ="Z"; // NUMBERS VAR NUMBEROFBOTTLES = 4; VAR COSTOFBOTTLES=2.99; // BOOLEAN VAR NUMBEROFBOTTLES = 4; VAR COSTOFBOTTLES=2.99; // NULL VAR OBJECT=NULL; // UNDEFINED VAR CUPS = UNDEFINED; VAR TEACUPS; |
Arrays 101... |
A couple of things to take into consideration.1. Unlike C++, Javascript allows you to avoid typing var as you declare the variables. Personally, I Always use var . You might run into problems when you are running or setting up functions. I will go over this in later tutorials.
2. Make sure you comment in your code. This will help you to remember how you approached the problem and other developers will not be scratching their heads while reading your code. 3. As you are learning JS, get used to the idea of checking the Console.log in Chrome. It is in developer mode. For one, you can spot errors and two you can test the code. Prompt()Prompt allows a user to insert information.
Not only you can insert the information but the prompt can spew back information back to the webpage. |
We will come back to this in depth a bit later. I can't throw everything at you at once...
Array - it is an ordered list of numerical index values. Here is how that'd done. 1. Declare a var. var cars = ["Volvo", "BMW", "Honda", "Toyota"]; You could also do this. var cars = [ cars0="Volvo", cars1="BMW", cars2="Honda", cars3="Toyota", ]; The count of the array starts at 0 not as 1. 2. Let's output this on the screen. Anytime you need to output Javascript code on your browser, you need to include document.getElementById("demo").innerHTML; The tag "demo" is the spot where you want the Javascript output to popup. 3. Let's say, you do not want people to see an entire list but 1st car. All you need to do is include document.getElementById("demo").innerHTML = cars[0]; |
<!DOCTYPE html>
<html> <body> <p id="demo"></p> <script> var cars = [ cars0="Volvo", cars1="BMW", cars2="Honda", cars3="Toyota", ]; document.getElementById("demo").innerHTML = cars[0]; </script> </body> </html> If you want an entire list, This is an entire code. <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var cars = [ cars0="Volvo", cars1="BMW", cars2="Honda", cars3="Toyota", ]; document.getElementById("demo").innerHTML = cars; </script> </body> </html> |
var orderId = 90001;
console.log(orderId); output 9001 console.log(typeof orderId); output number var orderId = 90001.99; console.log(typeof orderId); output number |
var orderId = "UF90001";
console.log(orderId); output UF90001 console.log(typeof orderId); output string output undefined
var isActive = true; console.log(true); console.log(typeof isActive); output boolean |
var orderId ;
console.log(orderId); output undefined var orderId = null; console.log(typeof orderId); output object object is empty because it has no property fields var orderId = 9090;
console.log(orderId); var orderId = 9091; console.log(orderId); output 9091 |
'use strict'orderId = 90001;
console.log(orderId); output 9001 'use strict';
orderId = 90001; console.log(orderId); output runtime error var is undefined |
Complex Objects
|