Datatypes and variables: Difference between revisions

From JSXGraph Wiki
(New page: =Data types= Though JavaScript is a [http://en.wikipedia.org/wiki/Weak_typing weakly typed] programming language, there are some data types between which JavaScript sometimes variables aut...)
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{JSTuTToC}}
=Data types=
=Data types=
Though JavaScript is a [http://en.wikipedia.org/wiki/Weak_typing weakly typed] programming language, there are some data types between which JavaScript sometimes variables automatically converts:
Though JavaScript is a [http://en.wikipedia.org/wiki/Weak_typing weakly typed] programming language, there are some primitive data types between which JavaScript sometimes variables automatically converts:
 
==null==
Null means ''variable has no value at all''. Do not confuse '''null''' with 0 (zero)! 0 is just a number, null means just no value or a empty or non-existent reference.
 
==undefined==
A value that is undefined is a value held by a variable right after it has been created and before a value has been assigned to it.
 
==boolean==
 
 
==number==


* '''null''': Null means ''variable has no value at all''. Do not confuse '''null''' with 0 (zero)! 0 is just a number, null means just no value or a empty or non-existent reference.
* '''undefined''': A value that is undefined is a value held by a variable right after it has been created and before a value has been assigned to it. The return value of a function which is actually not returning a value, is of type undefined.
* '''boolean''': A variable of type boolean may hold the special values ''true'' or ''false''. If a number value is used where a boolean expression is expected, a zero value will be treated like ''false'' and any other value will be treated as ''true''.
* '''number''':  This type is a set of values representing integer and floating point numbers. In ECMAScript, the set of values represents the double-precision [http://en.wikipedia.org/wiki/IEEE_754 64-bit format IEEE 754] values including the special values ''Not-a-Number'' (NaN), ''positive infinity'' (Infinity), and ''negative infinity'' (-Infinity). Calculating with numbers is done like in C(++/#), Java, Python, Pascal, PHP, and similar languages.
* '''string''': A variable of type string is - formally spoken - a finite ordered sequence of at least zero 16-bit unsigned integer values. Practically it is just a string, like you may know from C++ or Java. In C it's like a character array, but with 16 bit characters instead of 8 bit C-char. Single characters of a string can be accessed just like in any other major C-like programming language using square brackets. The first character has index 0. Concatenating strings is done similar to C++/C#, Java, and Pascal with the '+' operator.


==string==
==Type conversion==

Latest revision as of 10:13, 16 July 2009

Introduction to JavaScript
  • Integrate JavaScript on webpages
    • Internal JavaScript
    • External JavaScript
    • Inline JavaScript
  • Datatypes and variables
    • Datatypes overview
    • Variables declaration
  • Arrays
    • Implementation
    • Declaration
    • array as object -> properties and methods
    • Anonymous arrays
    • associative arrays/hashtables
  • Functions
    • Declaration
    • Parameters
    • Default values for parameters and variable length parameter lists
    • return values
    • callback functions
    • anonymous functions
  • Objects
    • Class definition, object initialisation, singletons
    • Prototyping concept and inheritance
    • Anonymous objects
    • Objects as associative arrays


Data types

Though JavaScript is a weakly typed programming language, there are some primitive data types between which JavaScript sometimes variables automatically converts:

  • null: Null means variable has no value at all. Do not confuse null with 0 (zero)! 0 is just a number, null means just no value or a empty or non-existent reference.
  • undefined: A value that is undefined is a value held by a variable right after it has been created and before a value has been assigned to it. The return value of a function which is actually not returning a value, is of type undefined.
  • boolean: A variable of type boolean may hold the special values true or false. If a number value is used where a boolean expression is expected, a zero value will be treated like false and any other value will be treated as true.
  • number: This type is a set of values representing integer and floating point numbers. In ECMAScript, the set of values represents the double-precision 64-bit format IEEE 754 values including the special values Not-a-Number (NaN), positive infinity (Infinity), and negative infinity (-Infinity). Calculating with numbers is done like in C(++/#), Java, Python, Pascal, PHP, and similar languages.
  • string: A variable of type string is - formally spoken - a finite ordered sequence of at least zero 16-bit unsigned integer values. Practically it is just a string, like you may know from C++ or Java. In C it's like a character array, but with 16 bit characters instead of 8 bit C-char. Single characters of a string can be accessed just like in any other major C-like programming language using square brackets. The first character has index 0. Concatenating strings is done similar to C++/C#, Java, and Pascal with the '+' operator.

Type conversion