Brief intro to Javascript
History
JavaScript, not to be confused with Java, was created in 10 days in May 1995 by Brendan Eich, then working at Netscape and now of Mozilla. JavaScript was not always known as JavaScript: the original name was Mocha, a name chosen by Marc Andreessen, founder of Netscape.
We aimed to provide a “glue language” for the Web designers and part time programmers who were building Web content from components such as images, plugins, and Java applets. We saw Java as the “component language” used by higher-priced programmers, where the glue programmers—the Web page designers—would assemble components and automate their interactions using [a scripting language].
Values & Types
- JavaScript has typed values, not typed variables.
- Variables are just containers.
- The following built-in types are available:
- String
- Number
- Boolean
- null and undefined
- Object
- Symbol (new to ES6)
var a;
typeof a; // "undefined"
a = "hello world";
typeof a; // "string"
a = 42;
typeof a; // "number"
a = true;
typeof a; // "boolean"
a = null;
typeof a; // "object" -- weird, bug
a = undefined;
typeof a; // "undefined"
a = { b: "c" };
typeof a; // "object"JavaScript provides a typeof operator that can examine a value and tell you what type it is:
Objects
The object type refers to a compound value where you can set properties (named locations) that each hold their own values of any type. This is perhaps one of the most useful value types in all of JavaScript.
var myObjeto = {
"uno": true,
"dos": "hola"
};var obj = {
a: "hello world",
b: 42,
c: true
};
obj.a; // "hello world"
obj.b; // 42
obj.c; // true
obj["a"]; // "hello world"
obj["b"]; // 42
obj["c"]; // truevar obj = {
a: "hello world",
b: 42
};
var b = "a";
obj[b]; // "hello world"
obj["b"]; // 42Accessing Objects Properties
Arrays
An array is an object that holds values (of any type) not particularly in named properties/keys, but rather in numerically indexed positions.
var myArray = [ "uno", "dos", "tres" ];The other object subtype you'll use all over your JS programs is a function:
Functions
function foo() {
return 42;
}
foo.bar = "hello world";
typeof foo; // "function"
typeof foo(); // "number"
typeof foo.bar; // "string"Truthy & Falsy
The specific list of "falsy" values in JavaScript is as follows:
-
"" (empty string)
-
0, -0, NaN (invalid number)
-
null, undefined
-
false
Any value that's not on this "falsy" list is "truthy."
== OR ===
- == checks for value equality
- === checks for both value and type equality
- == checks for value equality with coercion allowed
- === checks for value equality without allowing coercion
var a = "42";
var b = 42;
a == b; // true
a === b; // falsevar a = [1,2,3];
var b = [1,2,3];
var c = "1,2,3";
a == c; // true
b == c; // true
a == b; // falseInequality
The <, >, <=, and >= operators are used for inequality, referred to in the specification as "relational comparison." Typically they will be used with ordinally comparable values like numbers. It's easy to understand that 3 < 4.
var a = 41;
var b = "42";
var c = "43";
a < b; // true
b < c; // truevar a = 42;
var b = "foo";
a < b; // false
a > b; // false
a == b; // falseBuilt-In Type Methods
The built-in types and subtypes we've just discussed have behaviors exposed as properties and methods that are quite powerful and useful.
For example:
var a = "hello world";
var b = 3.14159;
a.length; // 11
a.toUpperCase(); // "HELLO WORLD"
b.toFixed(4); // "3.1416"Javascript
By Cesar Guerrero Rincon
Javascript
- 193