JavaScriptAbout JavaScriptUsing JavaScript in a browserJavaScript isn’t always availableWhere to put JavaScriptPrimitive data typesVariablesOperators, IOperators, IICommentsStatements, IStatements, IIJavaScript is not JavaException handling, IException handling, IIObject literalsThree ways to create an objectArray literalsFour ways to create an arrayThe length of an arrayArrays and objectsArray functionsThe for…in statementThe with statementFunctionsRegular expressionsWarningsEvaluation (i.e., Dave’s opinion)The EndJan 13, 2019JavaScriptLanguage Fundamentals2About JavaScriptJavaScript is not Java, or even related to JavaThe original name for JavaScript was “LiveScript”The name was changed when Java became popularNow that Microsoft no longer likes Java, its name for their JavaScript dialect is “Active Script”Statements in JavaScript resemble statements in Java, because both languages borrowed heavily from the C languageJavaScript should be fairly easy for Java programmers to learnHowever, JavaScript is a complete, full-featured, complex languageJavaScript is seldom used to write complete “programs”Instead, small bits of JavaScript are used to add functionality to HTML pagesJavaScript is often used in conjunction with HTML “forms”JavaScript is reasonably platform-independent3Using JavaScript in a browserJavaScript code is included within <script> tags:<script type="text/javascript"> document.write("<h1>Hello World!</h1>") ;</script>Notes:The type attribute is to allow you to use other scripting languages (but JavaScript is the default)This simple code does the same thing as just putting <h1>Hello World!</h1> in the same place in the HTML documentThe semicolon at the end of the JavaScript statement is optionalYou need semicolons if you put two or more statements on the same lineIt’s probably a good idea to keep using semicolons4JavaScript isn’t always availableSome old browsers do not recognize script tagsThese browsers will ignore the script tags but will display the included JavaScriptTo get old browsers to ignore the whole thing, use: <script type="text/javascript"> <!-- document.write("Hello World!") //--> </script>The <!-- introduces an HTML commentTo get JavaScript to ignore the HTML close comment, -->, the // starts a JavaScript comment, which extends to the end of the lineSome users turn off JavaScriptUse the <noscript>message</noscript> to display a message in place of whatever the JavaScript would put there5Where to put JavaScriptJavaScript can be put in the <head> or in the <body> of an HTML documentJavaScript functions should be defined in the <head>This ensures that the function is loaded before it is neededJavaScript in the <body> will be executed as the page loadsJavaScript can be put in a separate .js file<script src="myJavaScriptFile.js"></script>Put this HTML wherever you would put the actual JavaScript codeAn external .js file lets you use the same JavaScript on multiple HTML pagesThe external .js file cannot itself contain a <script> tagJavaScript can be put in an HTML form object, such as a buttonThis JavaScript will be executed when the form object is used6Primitive data typesJavaScript has three “primitive” types: number, string, and booleanEverything else is an objectNumbers are always stored as floating-point valuesHexadecimal numbers begin with 0xSome platforms treat 0123 as octal, others treat it as decimalSince you can’t be sure, avoid octal altogether!Strings may be enclosed in single quotes or double quotesStrings can contains \n (newline), \" (double quote), etc.Booleans are either true or false0, "0", empty strings, undefined, null, and NaN are false , other values are true7VariablesVariables are declared with a var statement:var pi = 3.1416, x, y, name = "Dr. Dave" ;Variables names must begin with a letter or underscore Variable names are case-sensitive Variables are untyped (they can hold values of any type)The word var is optional (but it’s good style to use it)Variables declared within a function are local to that function (accessible only within that function)Variables declared outside a function are global (accessible from anywhere on the page)8Operators, IBecause most JavaScript syntax is borrowed from C (and is therefore just like Java), we won’t spend much time on itArithmetic operators (all numbers are floating-point): + - * / % ++ --Comparison operators: < <= == != >= >Logical operators: && || ! (&& and || are short-circuit operators)Bitwise operators: & | ^ ~ << >> >>>Assignment operators: += -= *= /= %= <<= >>= >>>= &= ^= |=9Operators, IIString operator: +The conditional operator: condition ? value_if_true : value_if_falseSpecial equality tests:== and != try to convert their operands to the same type before performing the test=== and !== consider their operands unequal if they are of different types Additional operators (to be discussed):new typeof void delete10CommentsComments are as in C or Java:Between // and the end of the lineBetween /* and */ Java’s javadoc comments, /** ... */, are treated just the same as /* ... */ comments; they have no special meaning in JavaScript11Statements, IMost JavaScript statements are also borrowed from CAssignment: greeting = "Hello, " + name;Compound statement: { statement; ...; statement }If statements: if (condition) statement; if (condition) statement; else statement;Familiar loop statements: while (condition) statement; do statement while (condition); for (initialization; condition; increment) statement;12Statements, IIThe switch statement: switch (expression) { case label : statement; break; case label : statement; break; ... default : statement; }Other familiar statements:break;continue;The empty statement, as in ;; or { }13JavaScript is not JavaBy now you should have realized that you already know a great deal of JavaScriptSo far we have talked about things that are the same as in JavaJavaScript has some features that resemble features in Java:JavaScript has Objects and primitive data typesJavaScript has qualified names; for
View Full Document