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:
nodeExpected 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
0to variableI.javascript I = 0;Checking the value of
Iresults in0.
Reassigning Values:
Example: Changing the value of
Ito10:javascript I = 10;Output now shows
10.Further reassigning value to
Ias45or 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
sumoutputs30.Case Sensitivity:
Explanation that
var Sumcreates a different variable thanvar sum.Attempting to access
Sumresults 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 4Examples of other math functions:
Ceiling Function:
Math.ceil(0.1)returns1.Floor Function:
Math.floor(0.9)returns0.Absolute Function:
Math.abs(-5)returns5.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 (returns3in 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
.lengthproperty:
var length = name.length; // returns 6Accessing 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.