x
Intro
Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a
scripting language
in a host environment,eg a browser, and it is up to the host environment to provide mechanisms for communicating with the outside world. JavaScript is an object-oriented dynamic language with types and operators, standard built-in objects, and methods.
JavaScript
borrows most of its syntax from Java, but is also influenced by Awk, Perl and Python. It is an interpreted programming language with object-oriented capabilities. The JavaScript programming language is
not
part of the Java platform. JavaScript does not create applets or stand-alone applications.
In its most common form, JavaScript resides inside HTML documents,
and can provide levels of interactivity to web pages that are not achievable with simple HTML. Client-side JavaScript does not allow the reading or writing of files. This has been kept for security reason. JavaScript cannot be used for networking applications because there is no such support available. JavaScript doesn't have any multithreading or multiprocessor capabilities. JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs. JavaScript, allows you to omit this semicolon if each of your statements are placed on a separate line. JavaScript is
case-sensitive
. JavaScript's
types
are: Number String Boolean Symbol (new in Edition 6) Object Function Array Date RegExp null undefined
Declarations
There are three kinds of declarations in JavaScript.
var
Declares a variable, optionally initializing it to a value.
let
Declares a block scope local variable, optionally initializing it to a value.
const
Declares a read-only named constant.
Javascript comment
//
is singleline comment Text between the characters
/*
and
*/
is treated as a comment.
Semicolons are optional
in JavaScript. To create a value, you must merely invoke its name. The main thing to do with numbers is arithmetic. Arithmetic operations such as addition or multiplication take two number values and produce a new number from them. The + and * symbols are called operators.
Booleans
The primitive boolean type comprises the values true and false. Boolean(12 > 4) // returns true
Script tag
Mostly, JavaScript runs in your web browser alongside HTML and CSS, and can be added to any web page using a script tag. The script tag takes two important attributes -
Language
This attribute specifies what scripting language you are using. You may find this attribute in out dated scripts.
It is obsolete and dont use it for JavaScript.
Type
This attribute is what is now recommended to indicate the scripting language in use and its value should be set to
"text/javascript".
The older HTML4 standard required this attribute to be set, but HTML5 allows it to be absent.
External scripts
Usually, most JavaScript code is put into an external file, which is attached to HTML, like this:
SCRIPT tags are not case sensitive but Javascript very definitely is.
alert("Hello World") works but Alert("Hello World") makes error. examples: alert("Hello World") confirm("OK or Cancel?") prompt("Favourite Colour?", "Red") <head>
<script type="text/javascript" src=filename.js><script>
</head> The type attribute is not required. JavaScript is the default scripting language in HTML. If src attribute is present then tag contents is ignored. Placing JavaScripts in external files has some advantages: 1 It separates HTML and code 2 It makes HTML and JavaScript easier to read and maintain cached JavaScript files can speed up page loads. Be sure that your JavaScript files (.js) do not include the <script> tag. They should only contain HTML commenting and JavaScript code. x
document.write
document.write("Hello World!") document.write can be used to write text, HTML, or a little of both.
var x;
// declaring a variable
x = 3 + y;
// assigning a value to the variable `x`
foo(x, y);
// calling function `foo` with parameters `x` and `y`
obj.bar(3);
// calling method `bar` of object `obj` // A conditional statement if (x === 0) { // Is `x` equal to zero? x = 123; }
Statements Versus Expressions
Statements do things. A program is a sequence of statements. Here is an example of a statement, which declares (creates) a variable foo:
var foo;
Expressions produce values. They are function arguments, the right side of an assignment, etc. Heres an example of an expression:
3 * 7
This statement writes Hello Dolly to the page: document.getElementById("demo").innerHTML = "Hello Dolly."; Use it like this: <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "Hello Dolly."; </script>

JavaScript Display Possibilities
JavaScript can "display" data in different ways: Writing into an alert box, using
window.alert()
. Writing into the HTML output using
document.write()
. Writing into an HTML element, using
innerHTML
. Writing into the browser console, using
console.log()
. Semicolons separate JavaScript statements.
Ending statements with semicolon is
not required, but highly
recommended.
JavaScript ignores multiple spaces. JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.

up