Web Development Guides and Tutorials

  • facebook
  • twitter
  • github

Javascript Basics 1 & 2

7th Sep 2015 9:40:31 Front End, Javascript by Kerry Owston

Javascript basics treehouse

Following the treehouse course Javascript Basics

Have picked up a few things in stage 1 & 2 of this 5 part course. Here are some details of the things I have been learning.

Checking for errors

Use consol in inspect element to figure out java script errors. 

Only one error at a time will show up when writing js so write one thing and then test it.

Send messages to console log by typing the following code:

console.log("text or variable here");

uncaught type error is a syntax error (missing quote mark or something)
something undeclered may mean the varable has been spelt incorectly.

Writing basic Javascript

When a varable is declared use a var before it but next time is it used there is no need to use the var see example below:

var example = "This is example content. ";
example += "This is more example content";

If adding to an existing varable this is possible by adding the varble name like example then adding a plus symbol then an equal symbol and then the new content to be added.

The way decribed is the short hand way to write javascript. The long hand way references the varable then assigns the varable the contents of itself and the uses a plus symbol before a string to assign the contents of he string.

See above exampe for this in action and below example for the long hand and short hand comapired to each other.

example = example + "this is the long way of adding to example varable";
example += "this is the short way of adding to example varable";

Use an alert box to alert users to important information. alert() is a function that that expects a string. The contents of this string will be popped up in an alert box on the users screen. 

alert("navigating away from this page will result in loss of progress")

A prompt box is similar to an alert box as it is a box that pops up in the users screen and is a ucntion that expects a string (or varable containing a string).

You can request input from the user with a prompt box, as an example you can ask your users name with the prompt function and what is contained between the prompt() parentesis will be the question asked of the user like what is your name? This example below will open a prompt box with the question and a space for the user to answer. The variable userName is the result of this user prompt so if the user types Kerry the varable username will be asigned the value Kerry.

var userName = prompt('What is your name?');

Pattern

I have noticed a pattern when writing javascript

I type as an example console.log() that is console period log parathasis.

evedent in other places too

console.log();
document.write();
userName.toUpperCase();

In these case the object is decribed first like the varable username and then it’s property (or at least i think that’s whats happenign here).