E: Type Values and Variables (part 3)

Introduction to Variables in JavaScript

  • Focus on the basics of variables as utilized in JavaScript, comparing it to Python for familiarity.

    • Commitment to understanding variables through a coding lens.

    • Set to use node.js for illustrating concepts directly.

Starting Node.js

  • Initialization of Node.js in Command Prompt:

    • Command: node

    • Expected output: Prompt allowing entry of JavaScript code.

Understanding Variables

  • Definition of Variables:

    • Used to store values for reuse in the program.

  • Declaring a Variable:

    • Example: Declaration of a variable I.
      javascript var I;

    • Immediate output after declaration: undefined.

    • Explanation: A variable declared without a value defaults to undefined.

Assigning Values to Variables

  • Assigning Initial Value:

    • Example: Assigning 0 to variable I.
      javascript I = 0;

    • Checking the value of I results in 0.

  • Reassigning Values:

    • Example: Changing the value of I to 10:
      javascript I = 10;

    • Output now shows 10.

    • Further reassigning value to I as 45 or a string is allowed, demonstrating JavaScript's dynamic typing.
      javascript I = "string value";

Variable Initialization

  • Initialization Upon Declaration:

    • Example: Declaring and initializing a variable sum:
      javascript var sum = 30;

  • To confirm, typing sum outputs 30.

    • Case Sensitivity:

    • Explanation that var Sum creates a different variable than var sum.

    • Attempting to access Sum results in a ReferenceError because it is not declared.

Multiple Variable Declaration

  • Declaring multiple variables simultaneously:

    • Example of declaration with a comma:
      javascript var I, sum;

    • Importance of scope and reassignment within their nearest context.

Mathematical Operations with Variables

  • Utilizing the Math Library:

    • Raising numbers and performing operations, e.g., calculating powers:
      javascript Math.pow(2, 2); // returns 4

    • Examples of other math functions:

    • Ceiling Function: Math.ceil(0.1) returns 1.

    • Floor Function: Math.floor(0.9) returns 0.

    • Absolute Function: Math.abs(-5) returns 5.

    • Max Function:
      javascript Math.max(28655, 1000); // returns 28655

Working with Dates

  • Creating a Date Object:

    • Example: Creating a variable to store the current date:
      javascript var date = new Date();

    • Accessing methods:

    • date.getFullYear() returns the current year (e.g., 2015).

    • date.getDate() obtains the day of the month (returns 3 in a provided example).

    • Clarification on getDay() results indicating the day of the week.

Strings in JavaScript

  • Declaring String Variables:

    • Examples using double and single quotes:
      javascript var name = 'Adrian'; var greeting = "Hello";

  • Using Escape Sequences in Strings:

    • Example to avoid conflicts with apostrophes within a string:
      javascript var message = 'It\'s a test';

String Operations

  • Concatenation of Strings:

    • Examples:
      javascript var message = "Hello" + " " + "World!";

  • Ensuring spaces between concatenated strings:
    javascript var spacedMessage = "Hello" + " " + "World!";

  • Finding Length of a String:

    • Example: Finding length using .length property:

    var length = name.length; // returns 6
    
  • Accessing Characters in a String:

    • Using the .charAt() method:
      javascript var firstChar = name.charAt(0); // returns 'A'

  • Substr and Slicing Strings:

    • Example of obtaining a substring:
      javascript var substring = name.substr(1, 4); // returns 'dria'

Equality and Comparison

  • Understanding Equality Operators:

    • Example of equality comparison:
      javascript console.log(1 == 1); // true console.log(1 == '1'); // true due to implicit conversion console.log(1 === '1'); // false due to strict equality

  • Not Equals Operator:

    • Examples:
      javascript console.log(2 != 2); // false console.log(2 != 4); // true

Conclusion and Next Steps

  • Summation of key points discussed regarding variables, math operations, string functions, and comparison mechanisms in JavaScript.

  • Moving forward to next sections focusing on expressions and operators in programming.

  • Encouragement to engage further with JavaScript coding for deeper understanding.