D: Type Values and Variables (part 2)

Lecture on Types, Values, and Variables (Part 2)

Introduction to Null and Undefined

  • Philosophical Inquiry:

    • Begins with a philosophical question about existence and value.

    • The main focus is on understanding the meaning of null and undefined in programming, specifically in JavaScript.

  • Concept of Null:

    • Definition: Null signifies the absence of a value.

    • Example:

    • When a variable is declared and assigned values like 0, 1, 2, …, it has a specific value.

    • If no value is assigned, null indicates that no value exists.

  • Concept of Undefined:

    • Definition: Undefined indicates that a variable has been declared but not initialized.

    • Example:

    • If a variable is declared using var i; without assignment, it will return undefined.

    • This signifies a deeper layer than null, signifying that initialization has not occurred.

  • Function Return Values:

    • In JavaScript, all functions return a value. If not explicitly returned, they default to undefined.

Differences Between Null and Undefined

  • Undefined:

    • Generally represents an unexpected situation in the code.

    • Indicates a variable that was expected to be initialized but wasn’t.

  • Null:

    • Represents a programmer's intent to signify that the current value is intentionally absent.

    • Used at the program level by assigning null to a variable, often followed by checks to determine if the value is null.

Global Object

  • Definition of Global Object:

    • The global object in JavaScript is a universally accessible object that is always present during the execution of a JavaScript program.

  • Attachments to Global Object:

    • Avoids pollution of the global namespace, but certain functions and symbols are attached, such as:

    • Mathematical Functions: Math, isNaN, parseInt, parseFloat, etc.

    • Date Object: e.g., new Date() is readily available.

    • Regular Expressions and JSON are also part of the global object.

Type Conversions in JavaScript

  • Implicit Type Conversion:

    • JavaScript performs implicit conversions which sometimes lead to unexpected results due to types being coerced silently.

    • Example of Implicit Conversion:

    • Adding a number and a string:

      • Statement: 10 + 'objects'

      • Result: 10 is converted to "10" and concatenated to yield the string "10 objects".

  • Handling Different Types:

    • Multiplying strings: "7" * "4"

    • Both strings convert to numbers which results in 28.

  • Expression Errors:

    • Attempting an operation that doesn't make sense (i.e., subtracting a string x from a number) results in NaN (Not a Number).

Explicit Type Conversion

  • Method toString():

    • Use toString() to explicitly convert values to strings.

    • Examples:

    • var n = 17; n.toString(2) converts 17 to its binary string representation: "10001".

  • Working with Decimal Precision:

    • Using toFixed() method:

    • n.toFixed(0): rounds to 0 decimal places.

    • n.toFixed(2): rounds to 2 decimal places, etc.

  • Parsing Functions:

    • The parseInt and parseFloat functions are globally available.

    • Examples of parsing:

    • parseInt("3 blind mics") returns 3.

    • parseFloat("3.14 meters") returns 3.14.

    • Parsing a negative float: parseFloat("-12.34") returns -12.34.

Conclusion

  • The lecture concludes with a note on the next topic: Variable Declarations.

  • Expresses anticipation for further discussions on the subject matter.