JavaScript Sheet1
JavaScript Sheet1
MCA Department
<script>
window.alert(5 + 6);
</script>
<script>
document.write(5 + 6);
</script>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
Variables are Containers for Storing Data
Automatically
Using var
Using let
Using const
JavaScript Values
Fixed values
Variable values
JavaScript Literals
The two most important syntax rules for fixed values are:
10.50
1001
"John Doe"
'John Doe'
JavaScript Variables
JavaScript uses the keywords var, let and const to declare variables.
let x;
x = 6;
JavaScript Operators
(5 + 6) * 10
let x, y;
x = 5;
y = 6;
JavaScript Expressions
let x, y;
x = 5 + 6;
y = x * 10;
var x, y;
x = 5 + 6;
y = x * 10;
JavaScript Comments
The rules for legal names are the same in most programming languages.
Cannot be Redeclared
let x = 0;
var x = 0;
Block Scope
Before ES6 (2015), JavaScript had Global Scope and Function Scope.
ES6 introduced two important new JavaScript keywords: let and const.
Variables declared inside a { } block cannot be accessed from outside the block:
Example
{
let x = 2;
}
// x can NOT be used here
Variables declared with the var keyword can NOT have block scope.
Variables declared inside a { } block can be accessed from outside the block.
Example
{
var x = 2;
}
// x CAN be used here
Redeclaring Variables
Redeclaring a variable inside a block will also redeclare the variable outside the block:
Example
<script>
var x = 10;
document.write(x)
{
var x = 2;
document.write(x)
}
document.write(x)
</script>
Redeclaring a variable using the let keyword can solve this problem.
Redeclaring a variable inside a block will not redeclare the variable outside the block:
<script>
let x = 10;
document.write(x)
{
let x = 2;
document.write(x)
}
document.write(x)
</script>
JavaScript Const
Example
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
Must be Assigned
JavaScript const variables must be assigned a value when they are declared:
Correct
const PI = 3.14159265359;
Incorrect
const PI;
PI = 3.14159265359;
A new Array
A new Object
A new Function
A new RegExp
Arithmetic Operators
Assignment Operators
Comparison Operators
String Operators
Logical Operators
Bitwise Operators
Ternary Operators
Type Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
++ Increment
-- Decrement
Assignment
let x = 10;
x += 5;
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
Note
Operator Description
== equal to
!= not equal
? ternary operator
Operator Description
|| logical or
! logical not
Note
Operator Description
typeof Returns the type of a variable
Note
Any numeric operand in the operation is converted into a 32 bit number. The result is
converted back to a JavaScript number.
The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed
numbers.
Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010
Example
<script>
let text1 = "A";
let text2 = "B";
let result = text1 < text2;
</script>
Example
<script>
Example
<script>
let text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2;
</script>
Example
<script>
let text1 = "What a very ";
text1 += "nice day";
</script>
1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object
1. An object
2. An array
3. A date
// Numbers:
let length = 16;
let weight = 7.5;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Booleans
let x = true;
let y = false;
// Object:
const person = {firstName:"John", lastName:"Doe"};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
// Date object:
const date = new Date("2022-03-25");
Note
let x = 16 + "Volvo";
Does it make any sense to add "Volvo" to sixteen? Will it produce an error or will it produce
a result?
When adding a number and a string, JavaScript will treat the number as a string.
Example
let x = 16 + "Volvo";
Example
let x = "Volvo" + 16;
JavaScript evaluates expressions from left to right. Different sequences can produce different
results:
Write the output of the following
JavaScript:
<script>
let x = 16 + 4 + "Volvo";
document.write(x)
let x = "Volvo" + 16 + 4;
document.write(x)
</script>
JavaScript has dynamic types. This means that the same variable can be used to hold different
data types:
Example
<script>
let x; // Now x is undefined
x = 5; // Now x is a Number
document .write(x)
x = "John"; // Now x is a String
document .write(x)
JavaScript Strings
Strings are written with quotes. You can use single or double quotes:
Example
// Using double quotes:
let carName1 = "Volvo XC60";
You can use quotes inside a string, as long as they don't match the quotes surrounding the
string:
Example
// Single quote inside double quotes:
let answer1 = "It's alright";