Alireza Afzal Aghaei
Graduate student at SBU
JavaScript, aka Mocha, aka LiveScript, aka JScript, aka ECMAScript, is one of the world's most popular programming languages.
JavaScript is a high-level, object-oriented, dynamic, untyped, and interpreted programming language.
Virtually every personal computer in the world has at least one JavaScript interpreter installed on it and in active use.
Alongside HTML and CSS, it is one of the three essential technologies of World Wide Web content production.
Position State | Position No. | Date |
Highest | #6 | Apr 2015 |
Lowest | #12 | Oct 2014 |
In 1995, there was a dilemma with form validation. Visitors were filling out forms. Sometimes a field wasn’t filled in correctly.
The real problem was the time it took to send this data, process it and then display a message back.
You would end up waiting at least 30 seconds just for a message to tell you that you forgot to fill in your phone number!
JavaScript was originally developed in 10 days in May 1995 by Brendan Eich, while he was working for Netscape Communications Corporation.
Although it was developed under the name Mocha, the language was officially called LiveScript.
At the same time , another programming language called Java became very well known and so Netscape decided to try to cash in on this by renaming the language built into their browser to JavaScript .
So... what is the difference between Java and JavaScript anyway?
JavaScript supports much of the structured programming syntax from C (if statements, while loops, switch statements, do while loops, etc.).
Except two partial
exception:
In JavaScript, an object is an associative array, augmented with a prototype. there are two syntactical ways to specify such a name:
JavaScript supports implicit and explicit delegation.
JavaScript borrows most of its syntax from Java, but also inherits from Awk and Perl, with some indirect influence from Self in its object prototype system.
Brendan Eich says:
JavaScript has no concept of input or output. It is designed to run as a scripting language in a host environment , and it is up to the host environment to provide mechanisms for communicating with the outside world.
in most of browsers you can use console, document or window object for input/output operations.
console.write('Hello World!');
window.alert('Hello World!');
document.write('Hello World!');
a = 1; // a variable
A = 10; // another variable!
firstName = "alireza";
lastName = "Afzal aghaei" // because of ASI technique
// not need to semi-colon;
// this is a single-line comment
/* and this is
multi-line
comment */
An identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). since ECMAScript 5 unicode characters can be used in identifiers.
Variables in JavaScript have no type attached, and any value can be stored in any variable. Variables are declared with a var statement.
var x, z; // value of x and z is "undefined"
var y = 2;
var a = 10, b = 11;
y = z = 1e5;
abstract | delete | function | new | this |
boolean | do | goto | null | throw |
break | double | if | package | throws |
byte | else | implements | private | transient |
case | enum | import | protected | true |
catch | export | in | public | try |
char | extends | instanceof | return | typeof |
class | false | int | short | var |
const | final | interface | static | void |
continue | finally | let | super | volatile |
debugger | float | long | switch | while |
default | for | native | synchronized | with |
Operator | Name |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
++ | Increment |
-- | Decrement |
Operator | Name |
= | Assignment |
+= | Addition Assignment |
-= | Subtraction Assignment |
*= | Multiplication Assignment |
/= | Division Assignment |
%= | Modulus Assignment |
Operator | Name |
== | equal to |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
? : | ternary operator |
Operator | Name |
&& | and |
|| | or |
! | not |
if (condition1) {
// statement;
} else if (condition2) {
// statement;
} else {
// statement;
}
switch(expression) {
case m:
// statement;
break;
case n:
// statement;
break;
default:
// statement;
}
for (init; condition; step) {
//statement;
}
while (condition) {
//statement;
}
do {
//statement;
} while (condition);
for (variable in object) {
//statement;
}
function functionName(parameter1, parameter2 = "defaultValue") {
// statement;
// return something if you want!
}
var f = function (a, b) {return a * b}; // anonymous function
var z = f(4, 3);
var sum = function() {
var x = 0;
for (var i = 0; i < arguments.length; ++i) {
x += arguments[i];
}
return x;
}
var result = sum(1, 2, 3); // variadic function
There are different ways to create new objects:
using an object literal (single object)
using the keyword new (single object)
using constructor (multiple objects)
Objects are Variables Containing Variables
var actor = {
firstName : "Leonardo",
lastName : "DiCaprio",
age : 42,
eyeColor : "blue"
};
// only one object!
var actor = new Object();
actor.firstName = "Leonardo";
actor.lastName = "DiCaprio";
actor["age"] = 42;
actor["eyeColor"] = "blue";
// only one object!
function Actor(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
} // now you can create multiple objects!
var myFavoriteActor = new Actor("Leonardo", "DiCaprio", 42, "blue");
var anotherActor = new Actor("Christian", "Bale", 42, "green");
var numbers = [10, 20, 30, "js"];
numbers.push(40) // [10, 20, 30, "js", 40]
numbers[0] // 10
numbers[3] // "js"
numbers[6]= 50 // [10, 20, 30, "js", 40, undefined, 50]
var lenOfArray = a.length // 7
Arrays are special kinds of objects.
try {
// Statements in which exceptions might be thrown
} catch(Exception) {
// Statements that execute in the event of an exception
} finally {
// Statements that execute afterward either way
}
throw new Error("Something went wrong."); // Custom error messages
with (Expression or Object){
// statements
}
This statement is not allowed in strict mode.
Strict mode makes several changes to normal JavaScript semantics.
eliminates some JavaScript silent errors by changing them to throw errors.
fixes mistakes that make it difficult for JavaScript engines to perform optimizations.
prohibits some syntax likely to be defined in future versions of ECMAScript.
By Alireza Afzal Aghaei