Grants-Web-Net

Content Table

Details
Category: JavaScript
Published: 11 December 2014
Hits: 2809

JavaScript Table of Content

Details
Category: JavaScript
Published: 10 December 2014
Hits: 3086

{article Content Table}{title} {text} {/article}

JavaScript Table of Content
Comment Variable Declaration JavaScript Reserved Words document.write( )
function Attached Javascript File Creating a String Literal Creating a Number Literal
toFixed Method (Number) Where to put Javascript The typeof Operator
JavaScript Table of Content

Comments

Details
Category: JavaScript
Published: 01 November 2014
Hits: 1054

{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
  • */

Variable Declaration

Details
Category: JavaScript
Published: 11 December 2014
Hits: 1006

{article Content Table}{title} {text} {/article}

  1. You declare variables in JavaScript with the var keyword.
  2. You can declare multiple variables at once.
  3. You can also declare a variable and assign it a value at the same time.
  4. Until you assign a value to a variable it is undefined.
  5. Local variables must be declared with the var keyword, otherwise they will become global variables.
  6. It is good coding practice, to put all declarations at the top of each script or function.
  7. A variable name cannot be one of the reserved words in JavaScript.
  8. The first letter of a variable name must be a letter or an underscore (_).
  9. A variable name cannot contain any space characters.
  10. Remember that variable names are case-sensitive.
  11. Remember that variable names are case-sensitive.
  12. Also, keep your variable names meaningful.
  13. Declare variables at the top of the function in which they are first used.

Follow the two rules below to create valid variable names:

  1. Start your variables with a letter, an underscore or a dollar sign.
  2. 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

  1. var z =2;
  2. var;
  3. var sum;


declare multiple variables

  1. var i, sum;


variable you can assign a value the time of initialization

  1. var i = 0, j = 0, k = 0;
  2. var message = "Hello World";



 

JavaScript Reserved Words

Details
Category: JavaScript
Published: 12 December 2014
Hits: 1257

{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

document.write( )

Details
Category: JavaScript
Published: 03 January 2015
Hits: 1204

{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">

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot;
    &quot;http://www.w3.org/TR/html4/strict.dtd&quot; &gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;title&lt;/title&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
    &lt;meta name=&quot;description&quot; content=&quot;&quot; /&gt;
    &lt;meta name=&quot;keywords&quot; content=&quot;&quot; /&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
    &lt;body&gt;
        &lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
        document.write(&quot;&lt;strong&gt;Hello World&lt;/strong&gt;&quot;);
        <br/>
        document.write(&quot;&lt;br/&gt;first Javascript program!&quot;);
        &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;


</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

function

Details
Category: JavaScript
Published: 03 January 2015
Hits: 1274

{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>

Attached Javascript

Details
Category: JavaScript
Published: 06 January 2015
Hits: 1064

{article JavaScript Table of Content}{title} {text} {/article}

  1. 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;">

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br>
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;<br>
&lt;head&gt;<br>
    &lt;title&gt;title&lt;/title&gt;<br>
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br>
    &lt;meta name=&quot;description&quot; content=&quot;&quot; /&gt;<br>
    &lt;meta name=&quot;keywords&quot; content=&quot;&quot; /&gt;<br>
    &lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;<br>
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot; /&gt;<br>
&lt;/head&gt;<br>
<br>
&lt;body&gt;<br>
    &lt;script language=&quot;javascript&quot;<br>
                    type=&quot;text/javascript&quot; <br>
                    src=&quot;external.js&quot;&gt;<br>
        &lt;/script&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>

</pre>



<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

  1. 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!

Creating a String Literal

Details
Category: JavaScript
Published: 09 January 2015
Hits: 1087

{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

Creating a Number Literal

Details
Category: JavaScript
Published: 09 January 2015
Hits: 1055

{article JavaScript Table of Content}{title} {text} {/article}

toFixed Method (Number)

Details
Category: JavaScript
Published: 09 January 2015
Hits: 1092

{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;">

&lt;script&gt;<br>
var num = new Number(123);<br>
var fix = num.toFixed();<br>
document.write(fix);<br>
document.write(&quot;&lt;br/&gt;&quot;);<br>
<br>
num = new Number(123.456);<br>
fix = num.toFixed(2);<br>
document.write(fix);<br>
&lt;/script&gt;<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;">

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;<br>
&lt;head&gt;<br>
    &lt;title&gt;title&lt;/title&gt;<br>
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br>
    &lt;meta name=&quot;description&quot; content=&quot;&quot; /&gt;<br>
    &lt;meta name=&quot;keywords&quot; content=&quot;&quot; /&gt;<br>
    &lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;<br>
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot; /&gt;<br>
&lt;/head&gt;<br>
<br>
&lt;body&gt;<br>
&lt;script&gt;<br>
var num = new Number(123);<br>
var fix = num.toFixed();<br>
document.write(fix);<br>
document.write(&quot;&lt;br/&gt;&quot;);<br>
<br>
num = new Number(123.456);<br>
fix = num.toFixed(2);<br>
document.write(fix);<br>
&lt;/script&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<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;">

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br>
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;<br>
&lt;head&gt;<br>
    &lt;title&gt;title&lt;/title&gt;<br>
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br>
    &lt;meta name=&quot;description&quot; content=&quot;&quot; /&gt;<br>
    &lt;meta name=&quot;keywords&quot; content=&quot;&quot; /&gt;<br>
    &lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot; /&gt;<br>
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot; /&gt;<br>
&lt;/head&gt;<br>
<br>
&lt;body&gt;<br>
&lt;script&gt;<br>
var num = new Number(123);<br>
var fix = num.toFixed();<br>
document.write(fix);<br>
document.write(&quot;&lt;br/&gt;&quot;);<br>
<br>
num = new Number(123.456);<br>
fix = num.toFixed(6);<br>
document.write(fix);<br>
&lt;/script&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<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

  1. Where to put Javascript
  2. The typeof Operator