JAVASCRIPT OUTPUT AND SYNTAX

View previous topic View next topic Go down

JAVASCRIPT OUTPUT AND SYNTAX

Post by Admin on Tue Jul 28, 2015 6:23 pm

JavaScript does not have any built-in print or display functions.


JavaScript Display Possibilities

JavaScript can "display" data in different ways:

  • Writing into an alert box, using window.alert().

  • Writing into the HTML output usingdocument.write().

  • Writing into an HTML element, using innerHTML.

  • Writing into the browser console, usingconsole.log().




Using window.alert()

You can use an alert box to display data:.

Example



Code:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>






Using document.write()

For testing purposes, it is convenient to usedocument.write():

Example



Code:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>





Using document.write() after an HTML document is fully loaded, will delete all existing HTML:

Example



Code:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<button onclick="document.write(5 + 6)">Try it</button>
</body>
</html>






[th][/th]
The document.write() method should be used only for testing.



Using innerHTML

To access an HTML element, JavaScript can use thedocument.getElementById(id) method.
The id attribute defines the HTML element. TheinnerHTML property defines the HTML content:

Example


Code:

<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>






[th][/th]
In our examples, we often use the innerHTML method (writing into an HTML element).



Using console.log()

In your browser, you can use the console.log() method to display data.
Activate the browser console with F12, and select "Console" in the menu.

Example


Code:

<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
console.log(5 + 6);
</script>
</body>
</html>
[size]

SYNTAX
JavaScript syntax is the rules, how JavaScript programs are constructed.
[/size]

[size]

JavaScript Programs

computer program is a list of "instructions" to be "executed" by the computer.
In a programming language, these program instructions are called statements.
JavaScript is a programming language.
JavaScript statements are separated by semicolon.

Example



[/size]
Code:
<html>
<head>
<title>
my javascript
</title>
</head>
<body>
<script type="text/javascript" />
var x = 5;
var y = 6;
var z = x + y;
document.write(z);
</script>
</body>
</html>
[size]
try it



[/size][th][/th]
In HTML, JavaScript programs can be executed by the web browser.




[size]

JavaScript Statements

JavaScript statements are composed of:
Values, Operators, Expressions, Keywords, and Comments.
[/size]

[size]

JavaScript Values

The JavaScript syntax defines two types of values: Fixed values and variable values.
Fixed values are called literals. Variable values are called variables.
[/size]

[size]

JavaScript Literals

The most important rules for writing fixed values are:
Numbers are written with or without decimals:
10.50

1001


Strings are text, written within double or single quotes:
"John Doe"

'John Doe'


Expressions can also represent fixed values:
5 + 6

5 * 10


[/size]

[size]

JavaScript Variables

In a programming language, variables are used to storedata values.
JavaScript uses the var keyword to define variables.
An equal sign is used to assign values to variables.
In this example, x is defined as a variable. Then, x is assigned (given) the value 6:
var x;

x = 6;


[/size]

[size]

JavaScript Operators

JavaScript uses an assignment operator ( = ) to assignvalues to variables:
var x = 5;
var y = 6;


JavaScript uses arithmetic operators ( + - *  / ) tocompute values:
(5 + 6) * 10


[/size]

[size]

JavaScript Keywords

JavaScript keywords are used to identify actions to be performed.
The var keyword tells the browser to create a new variable:
var x = 5 + 6;
var y = x * 10;


[/size]

[size]

JavaScript Comments

Not all JavaScript statements are "executed".
Code after double slashes // or between /* and */ is treated as a comment.

Comments are ignored, and will not be executed:
var x = 5;   // I will be executed

// var x = 6;   I will NOT be executed


[/size]

[size]

JavaScript is Case Sensitive

All JavaScript identifiers are case sensitive
The variables lastName and lastname, are two different variables.
lastName = "Doe";
lastname = "Peterson";


JavaScript does not interpret VAR or Var as the keyword var.

[/size][th][/th]
It is common, in JavaScript, to use camelCase names.
You will often see names written like lastName (instead of lastname).




[size]

JavaScript Character Set

JavaScript uses the Unicode character set.
Unicode covers (almost) all the characters, punctuations, and symbols in the world.
For a closer look, please study our [url=../charsets/ref_html_utf8.html]Complete Unicode Reference[/url].[/size]
avatar
Admin
Admin
Admin

Posts : 83
Points : 7847
Reputation : 3
Join date : 2015-07-22

View user profile http://droid.1talk.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum