Eloquent Javascript: Values + Program Structure

devfish·2022년 12월 19일
0

Javascript

목록 보기
2/30

Ch2: Program Structure (D3)

Expressions / Statements

  • Expression: A fragment of code that produces a value (sentence fragment)
  • Statement: (full sentence)
  • Bindings: grasp values, changeable
  • Functions: have side effects (print), returns values

Keywords / reserved words not available for binding names

delete do else enum export extends false finally for
function if implements import interface in instanceof let
new package private protected public return static super
switch this throw true try typeof var void while with yield

Number.isNaN function

  • returns true only if argument given is not a valid number

Conditional & Looping

For loops

  • first semicolon initializes the loop, the second checks whether it should continue, the third updates the state of the loop after every iteration
  • break can be used to break out of the loop
  • continue jumps out of the body to continue to the next iteration
  • shortcuts: counter *= 1, counter++,counter--

Switch

switch (prompt("What is the weather like?")) {
  case "rainy":
    console.log("Remember to bring an umbrella.");
    break;
  case "sunny":
    console.log("Dress lightly.");
  case "cloudy":
    console.log("Go outside.");
    break;
  default:
    console.log("Unknown weather type!");
    break;
}

Capitalization

  • Most follow camelCase
  • Constructors, however, are generally capitalized

Ch1: Values

Numbers

  • 64 bits per number (2^64) - fractional digital numbers should be treated as approximations
  • +, -, *, / / % are called operators
    • typeof is also an operator (unary, bc it only takes one number)
    • minus is both unary & binary (e.g. - (10 - 2))
  • NaN: Not a Number (although it is a value of a number type) e.g. 0/0, Infinity - Infinity
    • the only value in JS not equal to itself

Strings

  • 16 bits per string (2^16 characters)
  • only backticks allow newlines (use \n or \t for new lines/tabs)
  • to indicate backslashes, enter double backslashes \ + \
  • backtick-quoted strings (template literals) can also embed other values (`${something}`)
  • they can be compared (e.g. console.log("Aardvark" < "Zoroaster") //true)
    • uppercase < lowercase, etc.

Logical Operators (&&, ||, !)

  • can be applied to boolean values (e.g. null || "user" // user, "Agnes" || "user" // Agnes)
    • ||: returns the value that is 'true' (the left if both are true)
    • &&: returns the value that is 'false'
  • precendence: > / == > && > || (guide for when to use parentheses)
  • ternary operator: a ? b : c (if a is true, b; if not, c)

Empty Values

absence of meaningful value (1. null; 2. undefined: yet to yield some value)

Automatic Type Conversions

0, NaN, "" count as false, all others as true

🌟 Using == or !=

when null or undefined occurs on either side of the operator, it produces true only if both sides are one of null or undefined. which is useful to test whether a value has a real value

console.log(null == undefined);
// → true
console.log(null == 0);
// → false

Used version of JS: 2017

Excerpts (Intro)

Like human languages, computer languages allow words and phrases to be combined in new ways, making it possible to express ever new concepts.

Mindset:

When you are struggling to follow the book, do not jump to any conclusions about your own capabilities. You are fine—you just need to keep at it. Take a break, reread some material, and make sure you read and understand the example programs and exercises. Learning is hard work, but everything you learn is yours and will make subsequent learning easier.

On programming complexity:

Keeping programs under control is the main problem of programming. When a program works, it is beautiful. The art of programming is the skill of controlling complexity. The great program is subdued—made simple in its complexity.

Some programmers believe that this complexity is best managed by using only a small set of well-understood techniques in their programs. They have composed strict rules (“best practices”) prescribing the form programs should have and carefully stay within their safe little zone.

This is not only boring, it is ineffective. New problems often require new solutions. The field of programming is young and still developing rapidly, and it is varied enough to have room for wildly different approaches. There are many terrible mistakes to make in program design, and you should go ahead and make them so that you understand them. A sense of what a good program looks like is developed in practice, not learned from a list of rules.

profile
la, di, lah

0개의 댓글