Codecademy JavaScript Basics Guide
Codecademy JavaScript Basics Guide
Primitive data types in JavaScript represent the simplest form of data and include numbers, strings, booleans, null, and undefined. These data types are immutable, meaning once a value is assigned, it cannot be changed. Numbers include integers and floating-point values, strings are sequences of characters, booleans represent true or false, null is the intentional absence of value, and undefined signifies a declared variable that hasn't been assigned a value .
JavaScript's arithmetic operators facilitate basic numerical operations: addition (+), subtraction (-), multiplication (*), division (/), and modulo (%). These operators allow for numeric calculations in programs. For instance, the expression 5 + 10 performs addition to result in 15, while 10 % 3 returns 1, representing the remainder of division .
In JavaScript, 'null' is explicitly assigned to represent no value and is used when a developer wants to indicate the intentional absence of an object value. In contrast, 'undefined' occurs when a variable is declared but not initialized. 'null' might be used as a placeholder in objects, while 'undefined' typically indicates that a variable has yet to be assigned a value .
JavaScript handles random number generation through the Math.random() function, which returns a floating-point number between 0 (inclusive) and 1 (exclusive). Additionally, Math.floor() can be used to obtain whole numbers by rounding down. For instance, Math.floor(5.95) results in 5 .
String interpolation in JavaScript, performed using template literals marked by backticks (`), allows embedding expressions within strings using the ${expression} syntax. This method makes code more readable and maintainable than traditional string concatenation, which requires multiple + operators. For example, `Hello, ${name}` is more straightforward than 'Hello, ' + name .
Comments enhance JavaScript code by providing explanations or annotations, making it easier to understand. Single-line comments use two forward slashes (//), while multi-line comments are enclosed within /* and */. These annotations do not affect the execution of code but serve as documentation for developers .
The console.log() method is vital for debugging and displaying output in JavaScript. It is used to log or print messages to the console, making it easier for developers to track the flow of a program and understand the state of variables during execution. This method can also print objects and other info to aid in debugging .
JavaScript variables contribute to code re-usability and readability by storing data that can be accessed or modified throughout a program. Declaring variables using var, let, or const helps ensure clear, maintainable code. 'var' is used in older JavaScript versions, 'let' allows reassignment of variables, and 'const' is used for variables that should not change. For instance, let name = "Tammy"; or const numberOfFingers = 20 ensures specific uses .
The assignment operator in JavaScript assigns a value to its left operand based on the value of its right operand. Examples include basic assignment (=), addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), and division assignment (/=). For instance, if let number = 100; using number += 10 increases number by 10, resulting in 110 .
Template literals improve JavaScript code by allowing more intuitive syntax for string operations and multi-line strings without concatenation. Unlike regular strings, which require + for concatenation and '\n' for new lines, template literals use backticks to embed expressions and maintain cleaner, more readable code. For example, `User has ${num} notifications` is more straightforward than 'User has ' + num + ' notifications' .