JavaScript-for-Web-Developers

Book notes for Professional JavaScript for Web Developers, 4th Edition By Matt Frisbie

View project on GitHub

Chapter 3. Language Basics

Case-Sensitivity

Everything is case sensitive var1 !== Var1

Identifiers

  • The first character must be a letter, underscore or dollar sign
  • All other characters may be letters (including extended ASCII or Unicode characters), underscores, dollars signs, or numbers
  • Convention dictates camelCasing identifiers

Comments

// Single lines or /* multi-line
*/

Statements

Statements are terminated with a semicolon. The book suggests “you should always include one,” but also states “omitting the semicolon makes the parser determine where the end of a statement occurs”. Modern convention is split and ultimately I believe this is a matter of organizational preference.

Curly braces can surround multiple statements to denote a code block.

Keywords

These are words defined by ECMA-262 for a specific use and not available for definition or modification break case catch class const continue debugger default delete do else export extends finally for function if import in instanceof new return super switch this throw try typeof var void while with yield

Variables

Variables are “loosely” typed. This means a variable can hold any type of data, the program does not have to define its type explicitly when the variable is created.

var

var someVar results in someVar being undefinded. Because this variable is loosely type, it is entirely valued to modify someVar so that

var someVar = 12; someVar = 'test'

Variables defined using the var operator are local to the function scope in which the variable was defined. If the keyword var is omitted the variable because global, but this is incredibly difficult to follow as a program scales.

var hoisting means that variables defined with var are “hoisted” to the top of a function’s scope. It is as if the variable is given a definition of undefined as the first line of the function’s scope.

let

let works very similarly to var but with some difference. let is block scoped as opposed to the function scope of var. This means that a let defined variable cannot be referenced outside of the block that it is defined in. Redundant variable definitions are also not allowed for variables defined by let. Also, hoisting is not available for let variables.

While var variables that are defined in the global context will attach themselves to the window object, let variables will not.

let declares a new variable in each iteration of a loop (for, for-in,for-of), thus resolving the “what will the var print after this for loops finishes executing” gotcha interview question

const

Const behaves the same way as let, except with one important difference. A const declaration must be initialized with a value and that value cannot be changed after declaration.

That said, a const object may have its properties modified after declaration: const someObj = {}; someObj.someProp = 'some new value';

Key Takeaways

  • Don’t Use var
  • Prefer const over let

Data Types

There are 7 data types in Javascript

  • Undefined
  • Null
  • Boolean
  • Number
  • String
  • Symbol
  • Object

typeof

The typeof operator returns a string value labeling the data type of the provide variable

Undefined is undefined

The Undefined type has the value undefined. This is the result of a variable being declared, but not initialized.

Null is null

The Null type has the value null. This is technically an empty pointer to an object. If defining a variable meant to hold an object, you should initialize said variable to null, not undefined

console.log(null == undefined)

While undefined should never purposefully be used, null has practical value as a placeholder.

Boolean

The boolean value has two and only two types, true and false. Falsy and truthy values (1 or 0 or "True") are Boolean equivalents, but only when casted through the Boolean() function.

Number

The number type supports both integers and floating-point values.
Floating point arithmetic leads to wear rounding errors, use with caution The largest and smallest numbers that the javascript engine can represent can be accessed through Number.MAX_VALUE and Number.MIN_VALUE. Anything beyond these numbers is stored as positive or negative Infinity

NaN

Not a Number is a numeric value that indicates an arithmetic expression failed to return a number. This is usually caused by a divide by zero. When used in any operation, NaN results in a value of NaN. NaN is also not equal to itself.

Number Conversions

JavaScript provides 3 functions for converting nonnumeric values into numbers.

Number()

This function can be used on any data type, but it has various, complicated rules based on the function parameter. Boolean values will result in a 0 or 1. null will return 0, but undefined will result in NaN. Strings that have a corresponding numeric value are assigned that value, otherwise they result in NaN

parseInt()

parseInt knows how to ignore whitespace as well as trailing non-numeric characters. If the first character provided is not numeric, parseInt returns NaN. Otherwise parseInt works as intuitively expected. It even contains a second argument (the radix) for hexadecimal and octal numeric values. Floating-point strings will only have their integer value returned. parseInt("12.9") === 12

parseFloat()

parseFloat works similarly to parseInt expect that floating-point numbers are correctly computed if they only contain one decimal point. parseFloat always ignores leading zeros, hexadecimal values will always return 0.

Strings

The String type is a sequence of 16-bit unicode characters. They can be represented by double quotes, single quotes, or backticks. Backticks allow for strings to be used as template literals. const x = 'my string'; console.log(take a look at ${x}) // take a look at my string

Symbols

The Symbol data type is new to the ECMAScript 6 definition. They are a guaranteed unique identify for object properties that do no risk property collision. These are provided to keep special properties unique amongst objects to avoid naming collisions.