- Details
- Category: JavaScript
- Hits: 2027
{article Content Table}{title} {text} {/article}
- Details
- Category: JavaScript
- Hits: 596
{article Content Table}{title} {text} {/article}
Comments
JavaScript supports two styles of comments. Any text between comment is ignored by JavaScript.
Single line comment
- // Single line comment.
Multiple lines comment
- /* Multiple lines
- * Comment
- * another Comment
- */
- Details
- Category: JavaScript
- Hits: 542
{article Content Table}{title} {text} {/article}
- You declare variables in JavaScript with the var keyword.
- You can declare multiple variables at once.
- You can also declare a variable and assign it a value at the same time.
- Until you assign a value to a variable it is undefined.
- Local variables must be declared with the var keyword, otherwise they will become global variables.
- It is good coding practice, to put all declarations at the top of each script or function.
- A variable name cannot be one of the reserved words in JavaScript.
- The first letter of a variable name must be a letter or an underscore (_).
- A variable name cannot contain any space characters.
- Remember that variable names are case-sensitive.
- Remember that variable names are case-sensitive.
- Also, keep your variable names meaningful.
- Declare variables at the top of the function in which they are first used.
Follow the two rules below to create valid variable names:
- Start your variables with a letter, an underscore or a dollar sign.
- After that, use as many letters, numeric digits, underscores or dollar signs as you like.
Syntax
var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];
Variables are declared with the var keyword
- var z =2;
- var;
- var sum;
declare multiple variables
- var i, sum;
variable you can assign a value the time of initialization
- var i = 0, j = 0, k = 0;
- var message = "Hello World";
- Details
- Category: JavaScript
- Hits: 545
{article Content Table}{title} {text} {/article}
JavaScript Reserved Words
The following are reserved words in JavaScript. They cannot be used as JavaScript variables, functions, methods, loop labels, or object names.
Reserved Words
abstract | arguments | boolean | break | byte |
case | catch | char | class* | const |
continue | debugger | default | delete | do |
double | else | enum* | eval | export* |
extends* | false | final | finally | float |
for | function | goto | if | implements |
import* | in | instanceof | int | interface |
let | long | native | new | null |
package | private | protected | public | return |
short | static | super* | switch | synchronized |
this | throw | throws | transient | true |
try | typeof | var | void | volatile |
while | with | yield |
- Details
- Category: JavaScript
- Hits: 553
{article Content Table}{title} {text} {/article}
document.write outputs text to the browser.
<br/>separate lines in the browser
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush: php; highlight: [13,14,15,16,17]; html-script: true">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd" >
<html>
<head>
<title>title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index,follow" />
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<script language="javascript" type="text/javascript">
document.write("<strong>Hello World</strong>");
<br/>
document.write("<br/>first Javascript program!");
</script>
</body>
</html>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
- Details
- Category: JavaScript
- Hits: 609
{article Content Table}{title} {text} {/article}
Javascript Function
The two functions were placed in the head scriptt; element while the function calls were placed right before the ending /body tag.<br>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd" >
<html>
<head>
<title>First Javascript</title>
<script language="javascript" type="text/javascript">
function sayBandName()
{
document.write("Journey rules!<br/>");
}
function sayBye()
{
document.write("Buh Bye!");
}
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
//Output to the browser
document.write("<strong>Hello World from Javascript.</strong>");
document.write("<br/>This is my first Javascript program!<br/>");
sayBandName();
sayBye();
</script>
</body>
</html>
- Details
- Category: JavaScript
- Hits: 494
{article JavaScript Table of Content}{title} {text} {/article}
- Create a new HTML document and save it
external.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index,follow" />
<link rel="stylesheet" type="text/css" href="/styles.css" />
</head>
<body>
<script language="javascript"
type="text/javascript"
src="/external.js">
</script>
</body>
</html>
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><br>
<head><br>
<title>title</title><br>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br>
<meta name="description" content="" /><br>
<meta name="keywords" content="" /><br>
<meta name="robots" content="index,follow" /><br>
<link rel="stylesheet" type="text/css" href="styles.css" /><br>
</head><br>
<br>
<body><br>
<script language="javascript"<br>
type="text/javascript" <br>
src="external.js"><br>
</script><br>
</body><br>
</html><br>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
- Create a new external javascript document and save it
external.js
document.write("Hello World attached Javascript file!<br/>");
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
document.write("Hello World attached Javascript file!<br/>");
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
Following is the output when the whole code listing is run:
Hello World attached Javascript file!
- Details
- Category: JavaScript
- Hits: 543
{article JavaScript Table of Content}{title} {text} {/article}
Just wrap some text in a pair of double or single quotation marks, and you have yourself a string.
Gluing Strings Together with the + Operator
- Details
- Category: JavaScript
- Hits: 522
{article JavaScript Table of Content}{title} {text} {/article}
- Details
- Category: JavaScript
- Hits: 559
{article JavaScript Table of Content}{title} {text} {/article}
Returns a string representation of a number in fixed-point notation, containing fractionDigits digits after the decimal point.
Example 1
<script>
var num = new Number(123);
var fix = num.toFixed();
document.write(fix);
document.write("<br/>");
num = new Number(123.456);
fix = num.toFixed(2);
document.write(fix);
</script>
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
<script><br>
var num = new Number(123);<br>
var fix = num.toFixed();<br>
document.write(fix);<br>
document.write("<br/>");<br>
<br>
num = new Number(123.456);<br>
fix = num.toFixed(2);<br>
document.write(fix);<br>
</script><br>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><br>
<head><br>
<title>title</title><br>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br>
<meta name="description" content="" /><br>
<meta name="keywords" content="" /><br>
<meta name="robots" content="index,follow" /><br>
<link rel="stylesheet" type="text/css" href="styles.css" /><br>
</head><br>
<br>
<body><br>
<script><br>
var num = new Number(123);<br>
var fix = num.toFixed();<br>
document.write(fix);<br>
document.write("<br/>");<br>
<br>
num = new Number(123.456);<br>
fix = num.toFixed(2);<br>
document.write(fix);<br>
</script><br>
</body><br>
</html><br>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
Output Esample 1
123
123.46
Example 2
<script>
var num = new Number(123);
var fix = num.toFixed();
document.write(fix);
document.write("<br/>");
num = new Number(123.456);
fix = num.toFixed(6);
document.write(fix);
</script>
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><br>
<head><br>
<title>title</title><br>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br>
<meta name="description" content="" /><br>
<meta name="keywords" content="" /><br>
<meta name="robots" content="index,follow" /><br>
<link rel="stylesheet" type="text/css" href="styles.css" /><br>
</head><br>
<br>
<body><br>
<script><br>
var num = new Number(123);<br>
var fix = num.toFixed();<br>
document.write(fix);<br>
document.write("<br/>");<br>
<br>
num = new Number(123.456);<br>
fix = num.toFixed(6);<br>
document.write(fix);<br>
</script><br>
</body><br>
</html><br>
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}
Output Example 2
123
123.456000