JavaScript is a high-level, interpreted programming language that is widely used for web development. It was originally developed by Brendan Eich at Netscape Communications Corporation in 1995. Since then, it has become an essential component of web development and is supported by all modern web browsers.
Before you can start writing JavaScript code, you'll need to set up your development environment. You can use any code editor of your choice, such as Visual Studio Code, Sublime Text, or Atom.
Once you have your development environment set up, you can start writing and running JavaScript code. Here's an example of a basic JavaScript program that prints a message to the console:
console.log("Hello, world!");This program uses the console.log() function to print the message "Hello, world!" to the console.
The console.log() function is a built-in function in JavaScript that outputs a message to the console.
In JavaScript, variables are used to store data values.
They are declared using the var, let, or const keyword.
Here's how each of these keywords works:
-
var: This keyword declares a variable with function scope or global scope, depending on where it is declared. Variables declared withvarcan be redeclared and reassigned within the same scope.Here's an example:
function myFunction() { var x = 10; if (true) { var x = 20; console.log(x); // Output: 20 } console.log(x); // Output: 20 }
In this example, the variable
xis declared withvarin the function scope. The if statement also declares a variable namedxwithin its block, which redeclares the variablexin the function scope. Whenconsole.log(x)is called within the if statement, it outputs20. Similarly, whenconsole.log(x)is called outside the if statement, it also outputs20. -
let: This keyword declares a variable with block scope. Variables declared withletcan be reassigned, but not redeclared, within the same scope.Here's an example:
function myFunction() { let x = 10; if (true) { let x = 20; console.log(x); // Output: 20 } console.log(x); // Output: 10 }
In this example, the variable
xis declared withletin the function scope. The if statement also declares a variable namedxwithin its block, but it does not redeclare the variablexin the function scope. Whenconsole.log(x)is called within the if statement, it outputs20. Whenconsole.log(x)is called outside the if statement, it outputs10. -
const: This keyword declares a constant variable with block scope. Variables declared withconstcannot be reassigned or redeclared within the same scope.Here's an example:
function myFunction() { const x = 10; if (true) { const x = 20; console.log(x); // Output: 20 } console.log(x); // Output: 10 }
The main difference between let and const in JavaScript is that variables declared with let can be reassigned, whereas variables declared with const cannot be reassigned.
Here's an example that illustrates the difference:
let x = 10;
x = 20;
console.log(x); // Output: 20
const y = 10;
y = 20; // Throws an error
console.log(y);In this example, we declare a variable x with the let keyword and a variable y with the const keyword.
We assign the value 10 to both variables. We then reassign the value 20 to x and try to reassign 20 to y.
When we try to reassign 20 to y, it throws an error because y is a constant and cannot be reassigned.
So in summary, if you have a variable that you know should not be reassigned, use const. If you have a variable that may need to be reassigned, use let.
Here's an example of how to declare a variable:
let name = "Ajay";This declares a variable named name and initializes it with the value "Ajay".
The let keyword is used to declare a variable in JavaScript.
JavaScript supports several data types, including strings, numbers, booleans, and objects. Here are some examples:
let message = "Hello";
let age = 22;
let isStudent = true;
let person = { name: "Ajay", age: 22 };In this example, message is a string, age is a number, isStudent is a boolean, and person is an object.
JavaScript also supports various operators, such as arithmetic operators, comparison operators, and logical operators.
Here are some examples:
let a = 10;
let b = 5;
let sum = a + b; // 15
let difference = a - b; // 5
let product = a * b; // 50
let quotient = a / b; // 2
let isEqual = a === b; // false
let isNotEqual = a !== b; // true
let isGreater = a > b; // true
let isLess = a < b; // false
let andOperator = true && false; // false
let orOperator = true || false; // true
let notOperator = !true; // falseIn this example, sum is the result of adding a and b, difference is the result of subtracting b from a, and so on.
That covers the basics of Day 1's concepts in JavaScript. Try practicing these concepts with some simple exercises to get comfortable with the language.
-
Number: This data type represents numeric values, both integers and floating-point numbers.
Here's an example:
let x = 10; let y = 3.14;
-
String: This data type represents text values, enclosed in single or double quotes.
Here's an example:
let name = "Ajay"; let message = "Hello, world!";
-
Boolean: This data type represents a logical value of true or false.
Here's an example:
let isRunning = true; let isLoggedIn = false;
-
Undefined: This data type represents a variable that has been declared but has no value assigned to it.
Here's an example:
let x; console.log(x); // Output: undefined
-
Null: This data type represents a variable that has been explicitly assigned the value null.
Here's an example:
let x = null; console.log(x); // Output: null
-
Arithmetic operators: These operators perform mathematical calculations.
Here's an example:
let x = 10; let y = 3; console.log(x + y); // Output: 13 console.log(x - y); // Output: 7 console.log(x * y); // Output: 30 console.log(x / y); // Output: 3.3333333333333335 console.log(x % y); // Output: 1
-
Assignment operators: These operators assign values to variables.
Here's an example:
let x = 10; x += 5; // Equivalent to x = x + 5 console.log(x); // Output: 15
-
Comparison operators: These operators compare two values and return a Boolean value of true or false.
Here's an example:
let x = 10; let y = 5; console.log(x > y); // Output: true console.log(x < y); // Output: false console.log(x >= y); // Output: true console.log(x <= y); // Output: false console.log(x === y); // Output: false console.log(x !== y); // Output: true
-
Logical operators: These operators perform logical operations and return a Boolean value of true or false.
Here's an example:
let x = 10; let y = 5; console.log(x > 5 && y < 10); // Output: true console.log(x > 5 || y < 2); // Output: true console.log(!(x > y)); // Output: false
-
String operators: These operators concatenate or combine strings.
Here's an example:
let firstName = 'Ajay'; let lastName = 'Dhangar'; console.log(firstName + ' ' + lastName); // Output: Ajay Dhangar
These are some of the basic data types and operators in JavaScript that you should know to get started with the language.
Now Go to practicals index.js