What is a Variable?
In JavaScript, a variable is a container for storing data values. Variables allow you to store, retrieve, and manipulate data throughout your code, making it possible to reuse and organize information efficiently.
How to Declare Variables
JavaScript provides three ways to declare variables:
Syntax for Declaring Variables
var name = "Alice"; // Using
var let age = 25; // Using let
const country = "USA"; // Using const
Example
let greeting = "Hello, world!";
console.log(greeting); // Output: Hello, world!
Questions & Answers:
JavaScript has several basic data types to represent different kinds of data. Here are the most common types:
Example of Data Types
let productName = "Laptop"; // String
let quantity = 5; // Number
let inStock = true; // Boolean
let discount = null; // Null
let color; // Undefined
Questions & Answers:
JavaScript includes various operators that perform calculations, comparisons, and logical operations.
1. Arithmetic Operators
These are used for performing mathematical calculations.
Example:
let x = 10;
let y = 5;
console.log(x + y); // Output: 15
console.log(x - y); // Output: 5
2. Assignment Operators
These are used to assign values to variables.
Example:
let a = 10;
a += 5; // Equivalent to a = a + 5
console.log(a); // Output: 15
Comparison Operators
These are used to compare values and return a Boolean (true or false).
Example:
console.log(5 == '5'); // Output: true (loose equality)
console.log(5 === '5'); // Output: false (strict equality)
Questions & Answers:
Strings in JavaScript represent text and offer a variety of methods for manipulation. Here’s how you can work with strings:
1. String Concatenation
Concatenate (combine) two or more strings using +.
let firstName = "Alice";
let lastName = "Smith";
let fullName = firstName + " " + lastName;
console.log(fullName); // Output: Alice Smith
2. Template Literals
You can also embed variables directly within strings using template literals with backticks ( ` ).
let age = 25;
let message = `I am ${age} years old.`;
console.log(message); // Output: I am 25 years old.
3. String Methods
Example:
let greeting = "Hello, World!";
console.log(greeting.length); // Output: 13
console.log(greeting.toUpperCase()); // Output: HELLO, WORLD!
Questions & Answers:
Let’s put it all together with a small project: building a simple calculator that performs addition, subtraction, multiplication, and division.
Exercise: Write code that:
Solution:
let num1 = 20;
let num2 = 5;
console.log("Addition:", num1 + num2); // Output: 25
console.log("Subtraction:", num1 - num2); // Output: 15
console.log("Multiplication:", num1 * num2); // Output: 100
console.log("Division:", num1 / num2); // Output: 4
Questions & Answers:
Recap:
Next Steps:
Questions & Answers for Recap:
GoInnovateSoftware
Copyright © 2024 GoInnovateSoftware - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.