JavaScript Tutorial For Beginners #7 – JavaScript Variables




Hey ninjas. In today’s JavaScript tutorial, we’ll be taking a look at what variables are from a birds eye view. Variables are at the heart of most programming languages, and JavaScript is no different in that respect.

Variables area a way of storing information (such as the pixel position of an element on a page, or the customer name, or email) and can be of any of the main JavaScript types (integers, strings, boolean values etc – we’ll cover these later).

We need to use variables in our JS programs, so that we can refer to different pieces of varying information for different parts of our scripts to run! For example, an image slider needs to store how many images are in the slider.

As always, fire away with any questions :).

SUBSCRIBE TO CHANNEL – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1

========== JavaScript for Beginners Playlist ==========

========== CSS for Beginners Playlist ==========

========== HTML for Beginners Playlist ==========

========== The Net Ninja ============

For more front-end development tutorials & to black-belt your coding skills, head over to – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg or http://thenetninja.co.uk

========== Social Links ==========

Twitter – @TheNetNinja – https://twitter.com/thenetninjauk

Original source


12 responses to “JavaScript Tutorial For Beginners #7 – JavaScript Variables”

  1. Net Ninja! Great energy! Your tuts are really fun to watch. I actually left a little question for you in tut38 of this series because that confused me. Could you please see if you can answer that? Also I have another little question please. I am trying to create an image slider( I have actually copied the code below). When I assign document.images part to the variable then my code wouldn't run. I can't work out why. Could you help? Thanks

    //this below code wouldn't work
    <body>
    <div><img src="image1.jpg" id="firstpic" alt="firstpic"></div>
    <script type="text/javascript">
    var imagecount=1;
    var x=document.images.namedItem("firstpic").src;
    function rotator(){
    if(imagecount<4){
    x="image"+imagecount+".jpg";
    imagecount++;
    }
    else{
    imagecount=1;
    }
    }
    setInterval(rotator,3000);

    </script>

    </body>

    //this below one works

    <body>
    <div><img src="image1.jpg" id="firstpic" alt="firstpic"></div>
    <script type="text/javascript">
    var imagecount=1;
    function rotator(){
    if(imagecount<4){
    document.images.namedItem("firstpic").src="image"+imagecount+".jpg";
    imagecount++;
    }
    else{
    imagecount=1;
    }
    }
    setInterval(rotator,3000);

    </script>

    </body>

  2. Another thing that could be done for the variable names if you're not intending to ever change their type would be to tack an abbreviation of what it should be onto the name, such as intFluffyKitten instead of fluffyKitten for an integer or strAppetizerChoice for a string. It gives a handy label for what data type it's intended to be.

    Of course, if you are intending on changing those variable's contents to a different type, that kinda tosses that out the window.

Leave a Reply