Javascript - Intro
Javascript - Intro
By Manish Sharma
Language timeline
category 1960s 1970s 1980s 1990s 2000s
scientific Fortran Matlab
business Cobol DBMSes SQL VB
functional Lisp ML, Scheme Erlang Haskell F#
imperative/ Algol Pascal, C, Ada, C++ Java C#
procedural Smalltalk
scripting BASIC Perl Python,
Ruby, PHP,
JavaScript
logical Prolog CLP(R)
2
What is JavaScript?
• created in 1995 by Brandon Eich of Netscape/Mozilla
"JS had to "look like Java" only less so, be Java’s dumb kid brother or
boy-hostage sidekick. Plus, I had to be done in ten days or something
worse than JS would have happened." - Brandon Eich
originally called "LiveScript" to match Netscape branding
renamed to JavaScript to capitalize on popularity of Java
submitted as a standard to ECMA in 1997 as "ECMAScript"
3
JavaScript today
• possibly the most used programming language today (!!)
mostly used for client-side web page scripting, but
increasingly used to build server apps, other programs
current standardized version: ECMAScript 5 (2009)
4
JavaScript vs. Java
• interpreted, not compiled + = JavaScript
dynamic typing
first-class functions; nested functions; closures
a structured, imperative object-oriented, scripting lang.
prototype-based object and inheritance system
sophisticated first-class resizable array type
first-class regular expression support
• more relaxed syntax and rules
fewer and "looser" data types
variables don't always need to be declared
key construct is first-class function rather than the class
5
Running JS code in a browser
<html>
<head>
<script src="myfile.js"
type="text/javascript"></script>
</head>
<body>
<p>My web page</p> ...
</body>
</html>
6
Running JS without a browser
• Node JS Server side: project started to create a standard
library of JS types and functions for all non-web apps
Node
V8 (Google / Chrome)
7
JavaScript syntax
Console.log
Console.log(expr, expr, ..., expr);
doesn't work in web browsers (use alert instead)
Use F12 key for open console
9
Variables
Var or let name = expression;
• Examples:
Var/let age = 32;
Var/let weight = 127.4;
Var/let clientName = "Connie Client";
• same operators: + - * / % ++ -- = += -= *= /= %=
similar precedence to Java
many operators auto-convert types: "2" * 3 is 6
11
Number properties/methods
Number object "static" properties
Number.MAX_VALUE largest possible number, roughly 10308
Number.MIN_VALUE smallest positive number, roughly 10-324
Number.NaN Not-a-Number; result of invalid computations
Number.POSITIVE_INFINITY infinity; result of 1/0
Number.NEGATIVE_INFINITY negative infinity; result of -1/0
13
Math properties/methods
Math.E e, base of natural logarithms: 2.718...
Math.LN10, Math.LN2, natural logarithm of 10 and 2;
Math.LOG2E, Math.LOG10E logarithm of e in base 2 and base 10
Math.PI , circle's circumference/diameter: 3.14159...
Math.SQRT1_2, Math.SQRT2 square roots of 1/2 and 2
Math.abs(n) absolute value
Math.acos/asin/atan(n) arc-sin/cosine/tangent of angle in radians
Math.ceil(n) ceiling (rounds a real number up)
Math.cos/sin/tan(n) sin/cosine/tangent of angle in radians
Math.exp(n) en, e raised to the nth power
Math.floor(n) floor (rounds a real number down)
Math.log(n) natural logarithm (base e)
Math.max/min(a, b...) largest/smallest of 2 or more numbers
Math.pow(x, y) xy, x raised to the yth power
Math.random() random real number k in range 0 ≤ k < 1
Math.round(n) round number to nearest whole number
Math.sqrt(n) square root 14
Comments (same as Java)
// single-line comment
/*
multi-line comment
multi-line comment
*/
15
Strings
var s = "Connie Client";
var firstName = s.substring(0, s.indexOf(" "));
var len = s.length; // 13
var s2 = 'Melvin Merchant'; // can use "" or ''
• === , !== are strict equality tests; checks type and value
"5.0" === 5 is false
20
The if/else statement
if (test) {
statements;
} else if (test) {
statements;
} else {
statements;
}
21
Boolean type
var iLike341 = true;
var ieIsGood = "IE6" > 0; // false
if ("JS is great") { ... } // true
if (0 || "") { ... } // false
• Examples:
0 || 42 || 12 || -1 returns 42 (truthy)
NaN || null || "" returns "" (falsey)
1 + 1 && 6 && 9 returns 9 (truthy)
3 && 4 && null && 5 && 6 returns null (falsey)
23
null vs. undefined
var ned = null;
var benson = 9;
var caroline;
do {
statements;
} while (test);
25
Functions
function name(paramName, ..., paramName) {
statements;
}
function myFunction(name) {
console.log("Hello, " + name + "!\n");
console.log("How are you?\n");
}
26
JavaScript keywords
break case catch continue debugger
default delete do else finally
for function if in instanceof
new return switch this throw
try typeof var void while
with
27