Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
JavaScript/JScript
(section)
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Variables=== Although not required, it is considered good practice to declare variables before using them. You do this using the '''var''' statement. The only time you must use the '''var''' statement is if you want to declare variables that are local to a function. Local variables are those that are only within the function. Re-declaring a variable will not change the value the variable already had. You can declare a variable implicitly (without using '''var''') by assigning a value to it. The first character must be a letter or an underscore (_), or a dollar sign ($). Variable names are case sensitive. In instances in which you want to declare a variable and initialize it, but without giving it any particular value, you may assign it a special value, null. If you declare a variable without assigning any value to it, it exists but is undefined. The types are: *String (can be treated as object) *Number (can be treated as object) *Boolean (can be treated as object) *Array (internally object) *Object *Null *Undefined. The type of a variable can be checked by: typeof(<var>); JS supports both integer and floating-point numbers. Integers can be positive, 0, or negative; a floating-point number can contain either a decimal point, an "e" (uppercase or lowercase), which is used to represent "ten to the power of" in scientific notation, or both. These numbers follow the IEEE 754 standard for numerical representation. Last, there are certain number values that are special: *NaN *positiv infinity *negativ infinity *positiv 0 *negativ 0 ====Declaration==== var x; var y = 10; // function scope, redeclaration allowed let a; let b = 10; // block scope, redeclaration not allowed As JS is a loosely-typed language, variables in JS technically have no fixed type. Instead, they have a type equivalent to the type of the value they contain. It is possible, under some circumstances, to force the automatic conversion (or coercion) of a variable or a piece of data into a different type. Numbers can easily be included in strings, but strings cannot be included directly in numbers, so explicit conversion functions, parseInt() and parseFloat(), are provided. var theFrom = 1; var theTo = 10; var doWhat = "Count from "; doWhat += theFrom + " to " + theTo + "."; After this code is executed, the ''doWhat'' variable contains "Count from 1 to 10." The number data have been coerced into string form. var nowWhat = 0; nowWhat += 1 + "10"; // In this case, because "10" is a string, // the "+=" operator concatenates. After this code is executed, the ''nowWhat'' variable contains "0110". The following steps are followed to arrive at this result: *Look at the types of 1 and "10". The "10" is a string, the 1 is a number, so the number is coerced into a string. *As the values on either side of the '''+''' operator are both strings, do a string concatenation. This results in "110" *Look at the types of the values on either side of the +=. ''nowWhat'' contains a number, and "110" is a string, so convert the number to a string. *As there are now strings on either side of the += operator, do a string concatentation. This results in "0110". *Store this result in ''nowWhat''. var nowThen = 0; nowThen += 1 + parseInt("10"); // In this case, "+=" performs addition. After this code is executed, the ''nowThen'' variable contains the integer 11. Um zu testen ob eine Variable x den Wert ‘undefined’ besitzt sind mir z.Z. zwei Verfahren bekannt. if (String(x) == "undefined") ... if (typeof(x) == "undefined") ...
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width