Beginner JS Pt. 2

Beginner JS Pt. 2Topics:

  • Strings
  • Variables
  • Arrays

Strings

What are strings? You've used them before, and you will continue using them through your whole JS career.

<script type="text/javascript">
alert('this is a string')
</script>

What are they? Typically, they are used after functions (such as alert) to list what appears in the function.

But constantly typing out strings can get annoying. If you chose to start coding in PBJS, you'll use the document.getElementsByTagName('td') function. Now, in even in the simplest codes, you'll need to put this in at least five times. It gets annoying I guarantee.

So you use a variable.

Variables

Variables are used to store data in JS, and typically take the place of strings.

How to write variables

var admin='Gunny'

The var part tells JS that this is a Global Variable, which means that it can be applied to any script below the variable declaration.

admin is the name of this variable. Variable names can only include letters, numbers and underscorces. They must start with a lowercase letter.

Some examples of good and bad variable names are below;

Good Variable Names

goodName

good_name

g80990oodname

goodname

goodname_69ers

Bad Variable Names

_badname

Badname

798Badname

Okay, so how do we use variables?

<script type="text/javascript">
var admin='Gunny is our beloved admin'
alert(admin)
</script>

Notice I didn't put admin in ' ' whereas I would have if it was a string. If I had put admin in ' ', the alert box would have come up with 'admin', thinking it was a string.

Note that variable names are case-sensitive, so JS reads helloworld and helloWorld as different variables.

Arrays

Arrays are specialist types of variables, used for storing data in JS. Just like variables! But better.

<script type="text/javascript">
var gunnysArray=new Array("South Park","The Simpsons","House")
</script>

See? They're like variables but instead you just storing one variable, they store as many as you want. Nice! How do we output them in a function though?

<script type="text/javascript">
var gunnysArray=new Array("South Park","The Simpsons","House")
document.write("My fav TV shows are"+gunnysArray);
</script>

Notice something new? I've used + to combine the array data with a string. You can do this with variables and Prompts too.

But what if I only want to return one of the above entries in the string? I would like to point out now that JS counts from zero, so you first value is 0, the second one is 1 etc etc.

You do this by adding a number after array name, surrounded by [ ]. So;

<script type="text/javascript">
var gunnysArray=new Array("South Park","The Simpsons","House")
document.write("My fav TV show is"+gunnysArray[0]);
</script>

This will write "My Fav TV show is South Park"


Related Articles

  • Beginner JS Pt. 2
    This tutorial is a well laid out recipe for basic Javascript. The article is brief and is limited to three topics: Calling the Script, Writing Comments, and Doing Stuff (Basic). Illustrations make it easy.
  • Niche Product Marketing: How To Elude Your Competition And Feed On Low Hanging Fruit
    One of the first horrible mistakes that newbie internetmarketers run into is jumping into the deep end without learning how to swim oreven stay afloat. This is a metaphor for tacking a niche that has ruthlesscompetitors with years of experience.
  • Beginner JS Pt. 1
    This tutorial is a well laid out recipe for basic Javascript. The article is brief and is limited to three topics: Calling the Script, Writing Comments, and Doing Stuff (Basic). Illustrations make it easy.
  • How a Beginner can Find a Domain Name and Hosting Service
    This quick guide to domain name web hosting will help you understand the process of hosting your domain name online. Finding and registering a domain name is one thing, but you still must host your domain name.
  • How To Choose The Best Web Hosting
    If youre like me, you have only a basic idea of what web hosting actually is: if you want to start a website, you need web hosting!
  • Understanding PPC (Pay Per Click)
    For the beginner, understanding PPC (Pay Per Click) services can be utterly confusing. With so many search engines to choose from, and so many options within each one the learning curve is quite substantial. So why would anyone go to the trouble?...
  • Building Your Website
    When you first start thinking about building a website, youll need to know there are several options available -
  • You Can Build Your Own Website
    There is really no reason why anyone wanting to have a website these days cannot build one on his or her own. Gone are the days when only high tech people possessed the ability to build amazing websites. Building a website on your own is fun, affordable and surprisingly easy.
  • A Beginner Guide to Web Hosting
    What is web hosting? Whenever you visit a website, what you see on your web browser is essentially just a web page that is downloaded from the web server onto your web browser. In general, a web site is made up of many web pages...
  • Top SEO Tips
    It is a fact that search engine optimization is very important. We all know that the internet is a changing arena, since it can get more and more competitive everyday.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.