1 / 105

Fundamental JavaScript [UTC, March 2014]

A bit of an introduction to JavaScript for University of Tennessee at Chattanooga’s Web 2 students.

Download Presentation

Fundamental JavaScript [UTC, March 2014]

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FUNDAMENTAL JAVASCRIPT Aaron Gustafson @AaronGustafson slideshare.net/AaronGustafson

  2. Variables (i.e. buckets)

  3. FUNDAMENTAL JAVASCRIPT Variables var my_var; var another_var, yet_another_var;

  4. FUNDAMENTAL JAVASCRIPT Variables var MYVAR, myvar, myVar, MyVar, MyVaR;

  5. FUNDAMENTAL JAVASCRIPT Variables: Scope function myFunc() { var my_var = false; } my_var; // undefined

  6. Data Types (i.e. stuf that goes in buckets)

  7. FUNDAMENTAL JAVASCRIPT Data type: Strings var single_quoted = 'my text', double_quoted = "more text"; var no_escape_necessary = 'some "text"', escaped = 'some \'text\''; var numeric_string = '06517';

  8. FUNDAMENTAL JAVASCRIPT Data type: Numbers var positive = 34, negative = -1, decimal = 3.14;

  9. FUNDAMENTAL JAVASCRIPT Data type: Booleans var yes = true, no = false, also_yes = 1, // truthy also_no = 0; // falsey

  10. FUNDAMENTAL JAVASCRIPT Data type: Arrays var my_cats = []; my_cats[0] = 'Sabine'; my_cats[1] = 'Dakota'; my_cats; // ['Sabine','Dakota']

  11. FUNDAMENTAL JAVASCRIPT Data type: Arrays var sabine = [ 'Sabine', // 0 = name 'cat', // 1 = type 'female', // 2 = gender 17, // 3 = age true // 4 = spayed/neutered ]; sabine[2]; // 'female'

  12. FUNDAMENTAL JAVASCRIPT Data type: Arrays var sabine = ['Sabine', 'cat', 'female', 14, true], dakota = ['Dakota', 'cat', 'male', 13, true]; pets = [ sabine, dakota ]; pets[1][0]; // 'Dakota'

  13. FUNDAMENTAL JAVASCRIPT Data type: Hashes var sabine = []; sabine['name'] = 'Sabine'; sabine['type'] = 'cat'; sabine['gender'] = 'female'; sabine['age'] = 14; sabine['fixed'] = true; sabine; // [] sabine['name']; // 'Sabine' sabine.name; // 'Sabine'

  14. FUNDAMENTAL JAVASCRIPT Data type: Objects var sabine = {}; sabine.name = 'Sabine'; sabine.type = 'cat'; sabine.gender = 'female'; sabine.age = 14; sabine.fixed = true; sabine; // Object sabine['name']; // 'Sabine' sabine.name; // 'Sabine'

  15. Operators (i.e. telling JS what to do)

  16. FUNDAMENTAL JAVASCRIPT Operators: Arithmetic var one = 2 - 1, two = 1 + 1, three = 9 / 3, four = 2 * 2, five = three + two;

  17. FUNDAMENTAL JAVASCRIPT Operators: Concatenation 'This is a ' + 'string'; // 'This is a string'

  18. FUNDAMENTAL JAVASCRIPT Operators: Shorthand var my_var = 1; my_var += 2; // 3 my_var -= 2; // 1 my_var *= 2; // 2 my_var /= 2; // 1 my_var++; // 2 (after eval.) my_var--; // 1 (after eval.) ++my_var; // 2 (before eval.) --my_var; // 1 (before eval.)

  19. FUNDAMENTAL JAVASCRIPT Operators: Comparison var my_var = 1; my_var > 2; // false my_var < 2; // true my_var == 2; // false my_var >= 2; // false my_var <= 2; // true my_var != 2; // true my_var === 2; // false my_var !== 2; // true

  20. FUNDAMENTAL JAVASCRIPT Operators: Identity function isTrue( value ) { return value === true; } isTrue( true ); // true isTrue( false ); // false isTrue( 1 ); // false isTrue( 0 ); // false

  21. FUNDAMENTAL JAVASCRIPT Operators: Logical if ( ! my_var ) { // my_var is false, null or undefined (not) } if ( my_var > 2 && my_var < 10 ) { // my_var is between 2 and 10 (exclusive) } if ( my_var > 2 || my_var < 2 ) { // my_var is greater or less than 2 // (i.e. my_var != 2) }

  22. FUNDAMENTAL JAVASCRIPT Operators: Logical if ( ! ( my_var < 2 ) ) { // my_var is not less than 2 // (or my_var >= 2) } if ( ( my_var > 2 && my_var < 10 ) || my_var == 15 ) { // my_var is between 2 and 10 (exclusive) // or my_var is 15 }

  23. FUNDAMENTAL JAVASCRIPT Data type: Dynamic typing var my_var = false; // boolean my_var = 14; // number my_var = "test"; // string my_var = []; // array my_var = {}; // object my_var = function(){}; // function

  24. FUNDAMENTAL JAVASCRIPT Data type: Dynamic typing 'This is a ' + 'string'; // 'This is a string' 10 + '20'; // '1020'

  25. Control Structures (i.e. conducting the symphony)

  26. FUNDAMENTAL JAVASCRIPT Conditional Action if ( condition ) { statement ; }

  27. FUNDAMENTAL JAVASCRIPT Semicolons: Use them first statement second statement

  28. FUNDAMENTAL JAVASCRIPT Semicolons: Use them first statement second statement compression first statement second statement

  29. FUNDAMENTAL JAVASCRIPT Semicolons: Use them first statement; second statement; compression first statement; second statement;

  30. FUNDAMENTAL JAVASCRIPT Conditional Action if ( 1 > 2 ) { console.log( 'something is terribly wrong' ); }

  31. FUNDAMENTAL JAVASCRIPT Conditional Action if ( 1 > 2 ) { console.log( 'something is terribly wrong' ); } else { console.log( 'everything is okay' ); }

  32. FUNDAMENTAL JAVASCRIPT Conditional Action console.log( 1 > 2 ? 'something is terribly wrong' : 'everything is okay' );

  33. FUNDAMENTAL JAVASCRIPT Conditional Action if ( height > 6 ) { console.log( 'you are tall' ); } else if ( height > 5.5 ) { console.log( 'you are of average height' ); } else { console.log( 'you are shorter than average' ); }

  34. FUNDAMENTAL JAVASCRIPT Conditional Action var msg = 'You are '; switch ( true ) { case height > 6: msg += 'tall'; break; case height > 5.5: msg += 'of average height'; break; default: msg += 'shorter than average'; break; } console.log( msg );

  35. FUNDAMENTAL JAVASCRIPT For Loops for ( initialization ; test condition ; alteration ) { statement ; }

  36. FUNDAMENTAL JAVASCRIPT For Loops for ( var i=1; i<=10; i++ ) { console.log( i ); } // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

  37. FUNDAMENTAL JAVASCRIPT For Loops for ( var i=1; i<=10; i++ ) { console.log( i ); } // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 var i = 1; for ( ; i<=10; ) { console.log( i++ ); } // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

  38. FUNDAMENTAL JAVASCRIPT While Loops initialization ; while ( test condition ) { statement ; alteration ; }

  39. FUNDAMENTAL JAVASCRIPT While Loops var i = 1; while ( i < 10 ) { console.log( i ); i += 2; } // 1, 3, 5, 7, 9 i; // 11

  40. FUNDAMENTAL JAVASCRIPT While Loops var i = 11; while ( i > 10 ) { console.log( i++ ); } // infinite loop (condition is always met)

  41. FUNDAMENTAL JAVASCRIPT While Loops var i = 10; while ( i ) { console.log( i-- ); } // 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

  42. Functions (i.e. reusable bundles of logic)

  43. FUNDAMENTAL JAVASCRIPT Functions function name( arguments ) { statements ; }

  44. FUNDAMENTAL JAVASCRIPT Functions function isTrue( value ) { return value === true; } isTrue( true ); // true isTrue( false ); // false isTrue( 1 ); // false isTrue( 0 ); // false

  45. FUNDAMENTAL JAVASCRIPT Functions function add( a, b ) { return a + b; } add( 1, 2 ); // 3 add( 4, 5 ); // 9 add( 1, 2, 3 ); // 3

  46. FUNDAMENTAL JAVASCRIPT Functions function add( a, b, c ) { return a + b + c; } add( 1, 2 ); // Not a number (NaN) add( 4, 5 ); // NaN add( 1, 2, 3 ); // 6

More Related