javascript #2 : valeurs, types & opérateurs
DESCRIPTION
Javascript #2 : valeurs, types & opérateursTRANSCRIPT
Javascript : Valeurs, types & opérateurs
1. Un programme javascript
1.1 Outils
Moteurs JavaScript pour le développement
Navigateur JSFiddleConsole
Les outils du développeur Javascript (1)
FireFox Chrome Chrome Canary
Les outils du développeur Javascript (2)
Sublime Text BracketsAtom.io
1.2 code javascript ?
Un fichier javascript
alert("Bazinga"); alert("Im not crazy."); alert("My mother had me tested");
Quelques commandes
alert("Suit up!"); console.log("Haaaaaave you met Ted?");
Commentaires
// Single Line Comments !/* Multi-line Comments */
1.3 Où mettre du javascript ?
JavaScript dans une page HTML (1)
<!doctype html> <html> <head></head> <body> <script> alert('Hello world!'); </script> </body> </html>
JavaScript dans une page HTML (2)
<!doctype html> <html> <head></head> <body> <script src="hello.js"></script> </body> </html>
2. Valeurs
2.1 Nombres
Définir un nombre
890 // entier 87.90 // décimal 2.9e3 // 2.9 * 10 * 10 * 10
Maximum & minimum (1)
[-253 … 253]
Maximum & minimum (2)
9007199254740992 + 1 == 9007199254740992; // true 0.2 + 0.4 == 0.6000000000000001; // true 1/7 + 1/7 + 1/7 + 1/7 + 1/7 + 1/7 + 1/7 != 1; // true
Nombres spéciaux (1)
Infinity // plus l'infini -Infinity // moins l'infini NaN // Not a Number
Nombres spéciaux (2)
Infinity + 1 == Infinity // true Infinity - Infinity == NaN // true 0/0 == NaN // true NaN + 1 == NaN // true
2.2 Chaînes de caractères
Définir une chaîne de caractères
"When I get sad I stop being sad and be AWESOME instead." 'True story!'
Caractère d'échappement
"There is only one god and his name is Death. And there is only one thing we say to Death: \"Not today.\"" !"A bear there was, a bear, a bear!\n All black and brown, and covered with hair!\n The bear! The bear!\n Oh, come, they said, oh come to the fair!\n The fair? Said he, but I'm a bear!\n All black, and brown, and covered with hair!" !"hodor \\ hodor \\ hodor"
2.3 valeurs spéciales
Booléens
true != false // true true == 1; // true false == 0; // true
Null & Undefined (1)
Undefined : primitive value used when a variable has not been assigned a value
Null : primitive value that represents the intentional absence of any object value
Null & Undefined (2)
undefined == null // true undefined != 0 // true null != 0 // true
3. Opérateurs
3.1 Manipulation
Arithmétique
3 + 2 == 5; 3 - 2 == 1; 3 * 2 == 6; 3 / 2 == 1.5; 3 % 2 == 1; !3 * 2 + 2 == 8;
Concaténation
"If you're committed enough, you can make any story work." + " I once told a woman I was Kevin Costner, and it worked because I believed it." + " - Saul Goodman »; !// If you're committed enough, you can make any story work. I once told a woman I was Kevin Costner, and it worked because I believed it - Saul Goodman
3.2 Comparaison
Numérique
3 > 2 // true 3 < 2 // false 8 == 9 // false 8 != 9 // true 9 >= 9 // true 9 <= 10 // true !9 == '9' // true 9 === '9' // false !'a' < 'u' // true; 'ab' < 'ba' // true 'a' < 'Z' // false
Logique
true && true // true true && false // false false && false // false !true || true // true true || false // true false || false // false !!true // false !false // true !3 + 4 == 7 && 2 * 8 > 10 // true
Ternaire
true ? 'yep' : 'nop' // yep false ? 'yeah' : 'noop' // noop
3.3 Types & transformations
Types de données
typeof 8 // number typeof 9.3 // number typeof NaN // number typeof 'toto' // string typeof true // boolean typeof null // object typeof undefined // undefined
Conversion de type
3 * null // 0 "3" - 1 // 2 "3" + 1 // 4 "cinq" * 3 // NaN "five" * 3 // NaN false == 0 // true
4. Constantes & Variables
Nommer une variable (1)
Aucun espace
Aucun mots clés réservés : break case catch continue debugger default delete do else false finally for function if implements in instanceof interface let new null package private protected public return static switch throw true try typeof var void while with yield this
Aucune ponctuation sauf ‘_’ et ‘$’
Ne doit pas commencer par un chiffre
Nommer une variable (2)
// Syntax valide var a; var b, c; var s_variable; var b2;
// Syntax invalide var 2a; var a:a; var function;
Affecter une variable
// Affecter une variable var a = 8, b = "toto", c = true, d; a = 2; console.log(a); // 2 console.log(b); // toto console.log(c); // true console.log(d); // undefined
Constantes
const a = 8; a = 2; console.log(a); // 8
Merci pour votre attention.
BibliographieEloquent JavaScript - Marijn Haverbeke http://eloquentjavascript.net
Dynamisez vos sites web avec Javascript ! - Johann Pardanaud & Sébastien de la Marck http://fr.openclassrooms.com/informatique/cours/dynamisez-vos-sites-web-avec-javascript JavaScript Fundamentals - Jeremy McPeak http://code.tutsplus.com/courses/javascript-fundamentals
Guide JavaScript - teoli, BenoitL, delislejm, Ame_Nomade, SphinxKnight https://developer.mozilla.org/fr/docs/Web/JavaScript/Guide
Javascript – MAX_INT: Number Limits - Vjeux http://blog.vjeux.com/2010/javascript/javascript-max_int-number-limits.html
Lost - Jeffrey Lieber, J. J. Abrams, Damon Lindelof http://abc.go.com/shows/lost
Crédits (1)
Person of interest - Jonathan Nolan, David Slack, Patrick Harbinson http://www.cbs.com/shows/person_of_interest/
Halt and Catch Fire - Christopher Cantwell, Christopher C. Rogers http://www.amctv.com/shows/halt-and-catch-fire
Utilities terminal Icon - kxmylo http://www.iconarchive.com/show/simple-icons-by-kxmylo/utilities-terminal-icon.html
Breaking bad - Vince Gilligan http://www.amctv.com/shows/breaking-bad
House of Cards - Beau Willimon https://www.facebook.com/HouseofCards
The Big Bang Theory - Chuck Lorre, Bill Prady http://www.cbs.com/shows/big_bang_theory/
Game of Thrones - David Benioff, D. B. Weiss http://www.hbo.com/game-of-thrones
The Wire - David Simon http://www.hbo.com/the-wire
Crédits (2)
Silicon Valley - Mike Judge http://www.hbo.com/silicon-valley
The Killing - Veena Sud http://www.amctv.com/shows/the-killing
Band of Brothers - Tom Hanks, Steven Spielberg http://www.hbo.com/band-of-brothers