mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-28 13:00:31 -04:00
fix(curriculum): Update JavaScript Variables and Data Types Review (#58679)
Co-authored-by: Jessica Wilkins <67210629+jdwilkin4@users.noreply.github.com>
This commit is contained in:
@@ -21,16 +21,16 @@ Data types help the program understand the kind of data it's working with, wheth
|
||||
- **Floating point**: A floating point number is a number with a decimal point. Examples include 3.14, 0.5, and 0.0001.
|
||||
- **String**: A string is a sequence of characters, or text, enclosed in quotes. `"I like coding"` and `'JavaScript is fun'` are examples of strings.
|
||||
- **Boolean**: A boolean represents one of two possible values: `true` or `false`. You can use a boolean to represent a condition, such as `isLoggedin = true`.
|
||||
- **Undefined and Null**: An undefined value is a variable that has been declared but not assigned a value. A null value is an empty value, or a variable that has intentionally been assigned a value of `null`.
|
||||
- **Undefined and Null**: An `undefined` value is a variable that has been declared but not assigned a value. A `null` value is an empty value, or a variable that has intentionally been assigned a value of `null`.
|
||||
- **Object**: An object is a collection of key-value pairs. The key is the property name, and the value is the property value.
|
||||
|
||||
Here, the `pet` object has three properties or keys: `name`, `age`, and `type`. The values are `Fluffy`, `3`, and `dog`, respectively.
|
||||
|
||||
```js
|
||||
let pet = {
|
||||
name: 'Fluffy',
|
||||
name: "Fluffy",
|
||||
age: 3,
|
||||
type: 'dog'
|
||||
type: "dog"
|
||||
};
|
||||
```
|
||||
|
||||
@@ -39,8 +39,8 @@ let pet = {
|
||||
In this example below, two symbols are created with the same description, but they are not equal.
|
||||
|
||||
```js
|
||||
const crypticKey1= Symbol('saltNpepper');
|
||||
const crypticKey2= Symbol('saltNpepper');
|
||||
const crypticKey1= Symbol("saltNpepper");
|
||||
const crypticKey2= Symbol("saltNpepper");
|
||||
console.log(crypticKey1 === crypticKey2); // false
|
||||
```
|
||||
|
||||
@@ -63,21 +63,21 @@ let cityName;
|
||||
- To assign a value to a variable, you can use the assignment operator `=`.
|
||||
|
||||
```js
|
||||
cityName = 'New York';
|
||||
cityName = "New York";
|
||||
```
|
||||
|
||||
- Variables declared using `let` can be reassigned a new value.
|
||||
|
||||
```js
|
||||
cityName = 'Los Angeles';
|
||||
cityName = "Los Angeles";
|
||||
console.log(cityName); // Los Angeles
|
||||
```
|
||||
|
||||
- Apart from `let`, you can also use `const` to declare a variable. However, a `const` variable cannot be reassigned a new value.
|
||||
|
||||
```js
|
||||
const cityName = 'New York';
|
||||
cityName = 'Los Angeles'; // TypeError: Assignment to constant variable.
|
||||
const cityName = "New York";
|
||||
cityName = "Los Angeles"; // TypeError: Assignment to constant variable.
|
||||
```
|
||||
|
||||
- Variables declared using `const` find uses in declaring constants, that are not allowed to change throughout the code, such as `PI` or `MAX_SIZE`.
|
||||
@@ -103,8 +103,8 @@ let alsoCorrect = "This is also a string";
|
||||
- Strings are immutable in JavaScript. This means that once a string is created, you cannot change the characters in the string. However, you can still reassign strings to a new value.
|
||||
|
||||
```js
|
||||
let firstName = 'John';
|
||||
firstName = 'Jane'; // Reassigning the string to a new value
|
||||
let firstName = "John";
|
||||
firstName = "Jane"; // Reassigning the string to a new value
|
||||
```
|
||||
|
||||
## String Concatenation in JavaScript
|
||||
@@ -112,26 +112,26 @@ firstName = 'Jane'; // Reassigning the string to a new value
|
||||
- Concatenation is the process of joining multiple strings or combining strings with variables that hold text. The `+` operator is one of the simplest and most frequently used methods to concatenate strings.
|
||||
|
||||
```js
|
||||
let studentName = 'Asad';
|
||||
let studentName = "Asad";
|
||||
let studentAge = 25;
|
||||
let studentInfo = studentName + ' is ' + studentAge + ' years old.';
|
||||
let studentInfo = studentName + " is " + studentAge + " years old.";
|
||||
console.log(studentInfo); // Asad is 25 years old.
|
||||
```
|
||||
|
||||
- If you need to add or append to an existing string, then you can use the `+=` operator. This is helpful when you want to build upon a string by adding more text to it over time.
|
||||
|
||||
```js
|
||||
let message = 'Welcome to programming, ';
|
||||
message += 'Asad!';
|
||||
let message = "Welcome to programming, ";
|
||||
message += "Asad!";
|
||||
console.log(message); // Welcome to programming, Asad!
|
||||
```
|
||||
|
||||
- Another way you can concatenate strings is to use the concat method. This method joins two or more strings together.
|
||||
- Another way you can concatenate strings is to use the `concat()` method. This method joins two or more strings together.
|
||||
|
||||
```js
|
||||
let firstName = 'John';
|
||||
let lastName = 'Doe';
|
||||
let fullName = firstName.concat(' ', lastName);
|
||||
let firstName = "John";
|
||||
let lastName = "Doe";
|
||||
let fullName = firstName.concat(" ", lastName);
|
||||
console.log(fullName); // John Doe
|
||||
```
|
||||
|
||||
@@ -140,7 +140,7 @@ console.log(fullName); // John Doe
|
||||
- The `console.log()` method is used to log messages to the console. It's a helpful tool for debugging and testing your code.
|
||||
|
||||
```js
|
||||
console.log('Hello, World!');
|
||||
console.log("Hello, World!");
|
||||
// Output: Hello, World!
|
||||
```
|
||||
|
||||
@@ -149,7 +149,7 @@ console.log('Hello, World!');
|
||||
- Semicolons are primarily used to mark the end of a statement. This helps the JavaScript engine understand the separation of individual instructions, which is crucial for correct execution.
|
||||
|
||||
```js
|
||||
let message = 'Hello, World!'; // first statement ends here
|
||||
let message = "Hello, World!"; // first statement ends here
|
||||
let number = 42; // second statement starts here
|
||||
```
|
||||
|
||||
@@ -201,7 +201,7 @@ let isLoggedin = true;
|
||||
console.log(typeof isLoggedin); // boolean
|
||||
```
|
||||
|
||||
- However, there's a well-known quirk in JavaScript when it comes to null. The `typeof` operator returns `object` for null values.
|
||||
- However, there's a well-known quirk in JavaScript when it comes to `null`. The `typeof` operator returns `object` for `null` values.
|
||||
|
||||
```js
|
||||
let user = null;
|
||||
|
||||
Reference in New Issue
Block a user