JavaScript/Basics: Difference between revisions
wikademia>Hampshire2004 Reworked grammar, added note on tags |
wikademia>Hampshire2004 Adding part of a conceivable second lesson |
||
| Line 34: | Line 34: | ||
<br> | <br> | ||
--[[User:Xora K Joken|Xora K Joken]] 19:18, 3 April 2007 (UTC) | --[[User:Xora K Joken|Xora K Joken]] 19:18, 3 April 2007 (UTC) | ||
==Second Lesson== | |||
This second lesson will introduce the topics of variables and operators. | |||
===Variables=== | |||
One of the things JavaScript is used for is the manipulation of data. In JavaScript, pieces of data can either be stored in variables or arrays. | |||
Variables are declared using the ''var'' keyword: | |||
var numValue; | |||
var textValue; | |||
var binaryValue; | |||
and can be given values when they are created: | |||
var numValue = 3; | |||
var textValue = "This is text."; | |||
var binaryValue = true; | |||
Multiple variables can even be declared at the same time: | |||
var numValue = 3, textValue = "This is text.", binaryValue = true; | |||
Variables in JavaScript are weakly typed, meaning that the types of individual variables are not determined by the programmer. Unlike many languages, JavaScript only provides a generic ''var'' rather than separate types for integers, floating point numbers, characters, and strings. For example, in the statement | |||
var x = 0; | |||
it is fairly obvious to any individual reading the code that x is a number. However, if the statement | |||
x = x + "This is text that is probably not a number"; | |||
were to appear later in the code, JavaScript would change the variable to a text variable. This is one of the reasons that JavaScript is referred to as weakly typed. | |||
====Arrays==== | |||
===Operators=== | |||
[[Category:JavaScript]] | [[Category:JavaScript]] | ||
[[Category:Basics]] | [[Category:Basics]] | ||