Claudia Chiriță . 2026/2027
JavaScript The Definitive Guide, David Flanagan
Understanding ECMAScript 6, Nicholas C. Zakas
...
ob = document.getElementById("identif")
...
ob.id, ob.className
ob.src<h1 style="color:pink; text-align:center;">...</h1>
ob.style.color, ob.style.textAlign<head>
<script type="text/javascript">
/* JavaScript code */
</script>
</head>
/* multiline
comment
*/
// single line comment JavaScript debugger in browser
a browser tab is represented by a window object (interface Window)
alert(message); // displays an alert box which
// contains a message and an OK button
prompt(text, default-text); // displays a dialog box
// that requires the user
// to introduce information
function name(param1, param2, ...)
{
function body
}
x = 23; //number
y = "abc"; //string
z = true; //boolean
var a; //undefined
cat = {name:"Tigger", color:black}; //object
var x = 42;
let x = 42;
const x = 42;
typeof(x);
// global scope
function fA (){
//scope A
function fB () {
// scope B
}
}
var x = 5; //global
function hoist() { if (x == 5) { var x = 10; }
alert(x); }
hoist(); // will print undefined
alert(x); // will print 5
~
var x = 5; //global
function hoist() { var x; // local variable
if (x == 5) { // undefined
var x = 10; }
alert(x); }
hoist(); // will print undefined
alert(x); // will print 5
var i = 4;
var r = 42.2;
x = "2" * 7; // 14
y = "2" + 7; // "27"
z = parseInt("2") + 7; // 9
t = "2" * "7"; // 14
u = 2 + 3 + "4"; // "54"
z = "2" + 3 + 4; // "234"
built-in object Math
Math.PI //=> 3.14
Math.pow(2,3) // => 8
Math.round(4.7) // => 5
Math.random() // between 0 and 1
Math.sqrt(-1) // => NaN
any value can be converted explicitly using the predefined object Boolean
var name = Boolean(value);
true === Boolean("truuue") // => true
false === Boolean("") // => true
string of characters written between ' ' or " "
var s = "Tigger Lilly";
var t = 'Tigger Lilly';
var fname = s.slice (0, s.indexOf(" ")); // 'Tigger'
var lname = s.slice(s.lastIndexOf(" ")+1,s.length); // 'Lilly'
properties and methods:
length, charAt(), indexOf(), lastIndexOf(), replace(),
split(), toLowerCase(), concat()
var x = "abcde";
alert(x[0]);
x[0] = 'v';
alert(x[0]); // => a
var x
x == undefined // true
typeof(x) // undefined
var x = null
typeof(x) // object !
null == undefined // true
null === undefined // false
var ob = {prop1: val1, prop2: val2, ... , propn: valn};
ob.prop1; // val1
ob["prop1"]; // val1
var student = { name: "Tigger",
mark1: 9,
mark2: 10,
grade: function(){
return (this.mark1 + this.mark2)/2;
}
}
student.name \\ "Tigger"
student.mark1 \\ 9
student.mark2 \\ 10
student.grade() \\ 9.5
student.grade \\ function
var a = {name: "Tigger"} // object
var b = a; // a and b refer the same memory area
b.name = b.name + " Lilly"; // both b and a are modified
alert(a.name); // "Tigger Lilly"
var s = "Tigger"; // string
var t = s; // t copies the value of s
t = t + " Lilly" ; // only t is modified
alert(s) // => "Tigger"
var cat = {name:"Tigger", color:"black", age:14}
var cat = new Object();
cat.name = "Tigger";
cat.color = "neagră";
cat.age = 14;
function cat(n, c, v) {
this.name = n;
this.color = c;
this.age = v;
}
var c1 = new cat("Tigger", "black", 14);
var c2 = new cat("Fluff", "white", 2);
global properties: return a simple value
global methods: called globally, not for an object
var v = new Array();
v[0] = "a"; v[1]= "b";
var v = new Array("a","b");
var v = ["hi", 2, [5,7]];
v = [6,4,7,3];
v.length; // 4
v.push(10); // => v = [6,4,7,3,10]
v.pop(); // => v = [6,4,7,3]
v.shift(); // => v = [4,7,3]
v.unshift(10); // => v = [10,4,7,3]
v.sort(); // => v = [3,4,6,7]
var s = "today is Monday";
var a = s.split(" "); // a = ["today","is","Monday"]
a.reverse(); // a = ["Monday","is","today"]
var s = a.join(‘/’); // s = "Monday/is/today"
function name(arg1, arg2, ..., argn) {
instructions;
[ return value; ] // optional
}
a JavaScript function can be called with a variable number of parameters
function sum(a,b) {
return a+b;
}
sum(2,3); // 5
sum(); // NaN
sum(2); // NaN
sum(3,4,1,5,6,7) // 7
any function can access the object "arguments" (similar to an array) that contains the values of the arguments with which the function was called
function fun() {
return arguments.length;
}
fun(2,"sss", 5); // 3
function func(a, b, c) {
console.log(arguments[0]); // expected output: 1
console.log(arguments[1]); // expected output: 2
console.log(arguments[2]); // expected output: 3
}
func(1, 2, 3);
function (arg1, ..., argn) {
instructions;
}
const fun = function () {
return arguments.length;
}
var f = (x) => x+1;
array.map(function(currentValue, index, arr), thisValue)
function f(x) {
return x+1;
}
var array = [1,2,3,4];
var array1 = array.map(f);
console.log(array1); // [2,3,4,5]
function g(x) {
return x * this.a;
}
var o1 = {a:2}, o2 = {a:3};
var array = [1,2,3,4];
var array1 = array.map(g,o1); // [2,4,6,8]
var array2 = array.map(g,o2); // [3,6,9,12]
array.forEach(function(currentValue, index, arr), thisValue)
array.forEach(function(currentValue, index, arr), thisValue)
function f(x, i) {
alert(i + " : " + x);
}
var array = ['monday', 'tuesday', 'wednesday', 'thursday'];
array.forEach(f);
var d = [1, 2, 3, 4];
d.forEach(function(v, i, a) {a[i] = v + 1;});
console.log(d); //[2,3,4,5];
array.filter(function(currentValue, index, arr), thisValue)
function check(word) {
return word.length < 6;
}
var animals = ["cat", "tapir", "platypus", "red panda"];
var smol = animals.filter(check);
console.log(smol); // ["cat", "tapir"]
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
function sum(total,val) {
return total+val;
}
var array = [1,2,3,4];
var s = array.reduce(sum);
console.log(s); // 10;
var cats = [{name:"Tigger",age:7},{name:"Whiskers",age:6},
{name:"Fluff",age:3},{name:"Kit",age:2},
{name:"Tom",age:6},{name:"Berlioz",age:4}];
var copy = cats.slice();
var oldCats = copy.filter((x) => (x.age >= 6))
.map((x) => (x.name+" is "+x.age+" years old"))
.forEach((x) => {alert(x)});
. .
(>\---/<)
,' `.
/ q p \
( >(_Y_)< )
>-' `-' `-<-.
/ _.== ,=.,- \ questions?
/, )` '( )
; `._.' `--<
: \ | )
\ ) ;_/
`._ _/_ ___.'-\\\
`--\\\