programming basics lec 3-2

Upload: venkata-vikas

Post on 03-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Programming Basics Lec 3-2

    1/2

    In many programming languages when you create a variable, you don't just give it a name but you mustalso say exactly what type of information is going to be stored in that variable. You must decidebeforehand if it's going to store an integer, meaning a whole number without anything after the decimalpoint, or perhaps a floating-point number which might have a value after the decimal point or perhaps youneed to store a single character, just one letter, or perhaps multiple characters, what's called a string, oris it a Boolean value, and that just means a value that can only be true or false, or it could be even morecomplex type of data, but you have to choose that data type.And once you have chosen it you are not allowed to change it, and that's what's known as a stronglytyped language. You can create as many variables as you want but each variable must be ofone particular type and that's actually enforced and it can cause your program to crash if you try to put thewrong type in a variable that wasn't designed for it, but in JavaScript we don't do that. JavaScript is what'scalled a weakly-typed language. We don't make a variable that's only for integers or a variable only forstrings.We just create a generic variable, an empty container, and then we can simply put whatever type of value

    we want in it. Now if I create this variable without giving it a value, it starts off as what JavaScript callsundefined.However, then I can use the name of the variable and the equal sign, or more formally theassignment operator, to set the value of myVariable = 200. Now if I wanted to, right after this I could usethe same format and set it equal to a string. That just means one more characters strung together.

    Now we need to use the double quotes here to say where the string begins and where it ends. Insidethose double quotes it can contain spaces and special characters. Now in JavaScript you can use eitherdouble or single quotes to mark the start and end of a string. But don't mix them. Don't start with a singlequote and close with a double quote. Now I tend to use double quotes because that's more common inother languages but you will see both ways. And we can also store what are called Boolean

    values. Boolean just means a value that's either true or false.We can use the word true or the word false and written this way, they do not need the quotes aroundthem. JavaScript understands the word true and false written all lowercase. They are considered part ofthe language. So we can create a variable as undefined, then put a number in it, then put a string in it,then a Boolean. Now that doesn't mean JavaScript doesn't care about the type of data you have. It does.It treats numbers differently from strings and strings differently from Boolean values. But any JavaScriptvariable can hold any of these values and even more complex ones besides, like arrays and objects andfunctions, but we'll get into those later on.

    Now you might think, well it sounds much easier to be a weakly-typed language where you can putanything anywhere. One issue with a weakly-typed language like this is there is no guarantee that thevariable you think you have contains the right kind of data. Most of the time when you're programming,you won't need or want to change the type of data that a variable stores. If you have a variable thatyou've called high score, well you would expect it to have a number in it, not the word pomegranate or thevalue true. If you have a variable called firstName you don't want it to contain the value of 74.5. And thereis nothing that would prevent that in JavaScript, but in other languages strong typing can have anadvantage by providing internal rules that stop a lot of common errors from happening.

  • 8/11/2019 Programming Basics Lec 3-2

    2/2

    Now of course, the point of making any variable is that we are going to use them, we are going tomanipulate them, to ask questions of them, but that allbegins by knowing how to make them.