var n = 25;== is the "tolerant" comparison operator. It returns true if the expressions are equal after converting then to number values. For example:
3 == "3"returns true because 3 and "3" are equal after converting both to numbers.
3 === "3"returns false because, although both expressions are equal to 3 when converted to numbers, they are different datatypes so the strict comparison operator declares them not equal.
var name = "Alice"; var greeting = "Hello, " + name + ", how are you?"; Answer: var name = "Alice"; var greeting = `Hello, ${name}, how are you?`;
function f(s, t) { return "*" + s + "*" + t + "*"; } console.log(f("Illinois", "Wisconsin")); // Output: *Illinois*Wisconsin*
return `*${s}*${t}*`;
"apple" "123" "!@#$%^&*()" " " (space) "" (null string = string of length zero)Single quotes can also be used for JavaScript strings:
'apple' '123' '' (null string = string of length zero) ' ' (space) '!@#$%^&*()'
var arr = ["dog", "cat", "mouse", "goldfish"];
charAt includes indexOf lastIndexOf repeat toUpperCaseAlso, test the string property length. Here is the interactive Node (REPL) script for testing these string methods:
> var s = "elephant"; undefined > s.charAt(4) 'h' > s.includes("eph") true > s.indexOf("eph") 2 > "abc".repeat(10) 'abcabcabcabcabcabcabcabcabcabc' > s.toUpperCase( ) 'ELEPHANT' > s.length 8
> var a = ["apple", "pear", "peach"]; undefined > var b = ["orange", "lemon"]; undefined > a.concat(b) [ 'apple', 'pear', 'peach', 'orange', 'lemon' ] > a.indexOf("pear") 1 > a.indexOf("kiwi") -1 > a.push("cherry") 4 > a [ 'apple', 'pear', 'peach', 'cherry' ] > a.sort( ) [ 'apple', 'cherry', 'peach', 'pear' ] > a.pop( ) 'pear' > a [ 'apple', 'cherry', 'peach' ] > a.length 3
function(n) { return 3 * n + 2; }This function cannot be used later.
var result = function(n) { return 3 * n + 2; }(5); console.log(result); // Output: 17This works in an HTML script. However, it does not work as a Node.js script. In either case, it is simpler just to write:
var result = 3 * 5 + 1; console.log(result); // Output: 17To save the anonymous function to use later, save it in a variable like this:
var f = function(n) { return 3 * n + 2; }Then you could test the function stored in the variable f like this:
console.log(f(5)); // Output: 17
function ftInToCm(feet, inches) { return (12 * feet + inches) * 2.54; }Rewrite the function as an anonymous function saved in a variable and test it. Answer:
var ftInToCm = function(feet, inches) { return (12 * feet + inches) * 2.54; } console.log(ftInToCm(5, 10)) // Output: 177.8
// Original script: var arr = ["ibm", "microsoft", "google", "oracle"]; function print(item) { console.log(item); } arr.forEach(print) // Using an anonymous function instead of the print function: var arr = ["ibm", "microsoft", "google", "oracle"]; arr.forEach(function(item) { console.log(item); });
f(x) = x * xMath arrow notation for defining a function:
f: x -> x * xor
f: x => x * x
function ftInToCm(f, i) { return (f * 12 + i) * 2.54; }or
var ftInToCm = function(f, i) { return (feet * 12 + i) * 2.54; }
var ftInToCm = (f, i) => (f * 12 + i) * 2.54;
// Traditional notation function printGreeting( ) { console.log( ); } // Arrow notation var printGreeting = ( ) => { console.log( ); } // Test statement that calls printGreeting in both notations: printGreeting( );
// Traditional notation function makeGreeting(name) { return `Hello, ${name}, how are you?`; } // Arrow notation var makeGreeting = name => `Hello, ${name}, how are you?`;
app.listen(200);Instead, use the http-status-codes module like this:
const HttpStatusCodes = require("http-status-codes"); ... app.listen(); response.writeHead(HttpStatusCodes.StatusCodes.OK, {"Content-Type": "text/html" });To run the code in main.js, first create a Node project and install the http-status-codes module:
> npm init ... Press ^C at any time to quit. package name: (simple-server-v2) version: (1.0.0) description: SimpleServer Example Version 2 entry point: (index.js) main.js test command: git repository: keywords: author: Stan Smith license: (ISC) About to write to <path_name>\package.json: { "name": "simple-server-v2", "version": "1.0.0", "description": "SimpleServer Example Version 2", "main": "main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Stan Smith", "license": "ISC" } Is this OK? (yes)npm means Node Package Manager.
> npm i http-status-codesThe option i means install.
const HttpStatusCodes = require("http-status-codes");Also, modify the script to use
response.writeHead(HttpStatusCodes.StatusCodes.OK, {"Content-Type": "text/html" });instead of
response.writeHead(200, {"Content-Type": "text/html" });
> node main.js
localhost:3000/