Into Programming
summary note from You Don't Know JS: Up & Going - hapter 1: Into Programming
Basics of Code
Code: A set of special instructions that instruct the computer to perform tasks.
Computer Language: Defines the valid format and combinations of instructions (syntax).
Statements: Groups of words, numbers, and operators that perform specific tasks.
Variables and Expressions
Variables: Symbolic placeholders for values.
Expressions: Combinations of variables, literals, and operators.
Operators: Symbols like
=,*for operations.
Example Statement
a = b * 2;aandbare variables, representing values.2is a literal value expression.bis a variable expression, retrieving its current value.b * 2is an arithmetic expression, performing multiplication.a = b * 2is an assignment expression, storing the result in variable a.
tells the computer, roughly, to get the current value stored in the variable
b, multiply that value by 2, then store the result back into another variable we calla
Program Execution
Executing: Running the program.
Interpreting: Running line by line.
Compiles: Runs ahead of time. JavaScript is compiled.
Output
console.log(b);printsbvalue to the console.alert(b);shows a pop-up alert withbvalue.
Input
age = prompt("Please tell me your age:");captures user input.
Operators
Arithmetic, increment/decrement, equality, comparison, logical, and assignment operators.
Math:
+(addition),-(subtraction),*(multiplication), and/(division)Increment/Decrement:
++(increment),--(decrement), as ina++(similar toa = a + 1).Equality: -
==(loose equals) -===(strict-equals) -!=(loose not-equals) -!==(strict not-equals)Comparison:
<(less than),>(greater than),<=(less than or loose-equals),>=(greater than or loose-equals), as ina <= bLogical:
&&(and),||(or), as ina || bthat selects eitheraorb.
The
=equals operator is used for assignmentRight-hand side (source value)
Left-hand side (target variable).
Values and Types
Primitive values are built-in types in JavaScript.
Converting Between Types
Explicit and implicit coercion.
explicit coercion
Number(..)(a built-in function)
implicit coercion
Example: Using
==converts the left-hand side from string to number for comparison.
Code Comments
Guidelines for effective comments.
//for one-line comments and/* .. */for multiline comments in JavaScript.
Variables
Dynamic typing in JavaScript allows variables to hold any type.
Constants are variables with values that don't change.
Constants are often capitalized with underscores between words.
Blocks
JavaScript uses
{ .. }for blocks.
Conditionals
ifstatements for conditional execution.
Loops
Iterations using
whileanddo..whileloops.Iterations typically start from
0in programming languages.
Functions
Functions take arguments and can return values.
Scope
Lexical scope determines variable accessibility within functions.
Only code inside that function can access that function's scoped variables.
code in one scope can access variables of either that scope or any scope outside of it.
Practice
Phone Purchase Program
Purchase Loop:
Use a loop to buy phones and accessories until out of money or reaching spending threshold.
Calculate Purchase Amount:
Calculate total purchase amount, including phones, accessories, and tax.
Print Formatted Purchase Amount:
Print the calculated purchase amount in a properly formatted manner.
Check Affordability:
Compare purchase amount with bank account balance to determine affordability.
Constants and Variables:
Set up constants for tax rate, phone price, and accessory price.
Create a variable for bank account balance.
Functions:
Define functions for calculating tax.
Define a function for formatting the price with a "$" and rounding to two decimal places.
Bonus Challenge:
Incorporate user input, such as prompting for the bank account balance, and spending threshold.
Encourage creativity and have fun with the implementation.
This chapter provides a comprehensive introduction to programming concepts, JavaScript syntax, and fundamental constructs. It covers variables, expressions, operators, control flow, functions, and more, laying a strong foundation for readers new to programming or JavaScript.
Last updated