Primitive data types: string, number, boolean, null, undefined
JavaScript has several primitive data types that are used to represent basic values. These primitive data types include:
- String: used to represent textual data. A string is a sequence of characters enclosed in single quotes (‘…’) or double quotes (“…”). Example:
let name = "John Doe";
- Number: used to represent numeric data. JavaScript has both integer and floating-point numbers. Example:
let age = 25;
- Boolean: used to represent logical values. A boolean value can be either
trueorfalse. Example:
let isStudent = true;
- Null: used to represent the intentional absence of any object value. Example:
let grade = null;
- Undefined: used to represent a variable that has not been assigned a value. A variable that has been declared but not initialized is undefined. Example:
let address;
These primitive data types are immutable, which means that their values cannot be changed once they are created.
Declaring and initializing variables
In JavaScript, variables are used to store data values. To declare a variable in JavaScript, you use the var, let, or const keyword followed by the variable name.
Here’s an example of declaring a variable using the var keyword:
var myVariable;
To initialize a variable with a value, you can use the assignment operator (=) followed by the value you want to assign to the variable. Here’s an example:
var myNumber = 42;
In this example, we declared a variable called myNumber and initialized it with the value 42.
The let and const keywords were introduced in newer versions of JavaScript (ES6), and they provide more control over how variables are declared and initialized.
With let, you can declare variables that have block scope, meaning they are only accessible within the block of code where they were declared. Here’s an example:
let myString = "Hello";
{
let myString = "World";
console.log(myString); // Output: World
}
console.log(myString); // Output: Hello
In this example, we declared two variables called myString. The first one is declared outside of a block and initialized with "Hello", while the second one is declared inside a block and initialized with "World". Because these variables have different scopes, they do not overwrite each other.
With const, you can declare variables that cannot be reassigned once they have been initialized. Here’s an example:
const PI = 3.14;
PI = 3; // Error: Assignment to constant variable.
In this example, we declared a constant variable called PI and initialized it with the value 3.14. When we try to reassign it with another value (3), we get an error because constants cannot be reassigned.
Overall, declaring and initializing variables in JavaScript is relatively simple but understanding their scope and behavior is crucial for writing effective code.
Converting data types
In JavaScript, you can convert data from one type to another using various built-in functions and operators. Here are some common methods for converting data types:
- String to Number
You can convert a string to a number using the parseInt() or parseFloat() functions. The parseInt() function converts a string to an integer, while the parseFloat() function converts a string to a floating-point number.
var myString = "42";
var myNumber = parseInt(myString);
console.log(typeof myNumber); // Output: "number"
In this example, we converted the string "42" to an integer using the parseInt() function.
- Number to String
You can convert a number to a string using the toString() method.
var myNumber = 42;
var myString = myNumber.toString();
console.log(typeof myString); // Output: "string"
In this example, we converted the number 42 to a string using the toString() method.
- Boolean to String/Number
You can convert a boolean value (true or false) to a string or number using their respective conversion functions (toString() and Number()).
var myBoolean = true;
var myString = myBoolean.toString();
console.log(typeof myString); // Output: "string"
var myNumber = Number(myBoolean);
console.log(typeof myNumber); // Output: "number"
In this example, we converted the boolean value true to both a string and number.
- Other Conversions
There are many other conversion methods available in JavaScript depending on what you need. For example, you can use the unary plus operator (+) to convert a string or boolean value to a number:
var myString = "42";
var myNumber = +myString;
console.log(typeof myNumber); // Output: "number"
In this example, we used the unary plus operator (+) to convert the string "42" into an integer.
Overall, understanding how to convert data types is important for working with different types of data in JavaScript and avoiding common programming errors related to type mismatches.
Tutorial: Basic variable manipulation
In JavaScript, variables are used to store and manipulate data values. Here’s a basic tutorial on how to manipulate variables in JavaScript:
Declaring Variables
To declare a variable in JavaScript, you use the var, let, or const keyword followed by the variable name.
var myVariable;
In this example, we declared a variable called myVariable.
Assigning Values to Variables
To assign a value to a variable, you can use the assignment operator (=) followed by the value you want to assign.
myVariable = 42;
In this example, we assigned the value 42 to the variable myVariable.
You can also declare and initialize a variable in one line of code:
var myNumber = 42;
In this example, we declared a variable called myNumber and initialized it with the value 42.
Manipulating Variables
Once you have assigned a value to a variable, you can manipulate its value using various operators and functions.
For example, you can use arithmetic operators (+, -, *, /) to perform mathematical operations on numeric variables:
var x = 10;
var y = 5;
var z = x + y; // z is now equal to 15
In this example, we added the values of x and y together using the addition operator (+) and stored the result in a new variable called z.
You can also concatenate strings using the concatenation operator (+):
var firstName = "John";
var lastName = "Doe";
var fullName = firstName + " " + lastName; // fullName is now equal to "John Doe"
In this example, we concatenated two string variables (firstName and lastName) together with a space character between them.
Conclusion
Overall, manipulating variables is an essential part of programming in JavaScript. By assigning values to variables and manipulating their values with operators and functions, you can create dynamic and interactive programs that respond to user input and provide useful output.