[go: up one dir, main page]

0% found this document useful (0 votes)
32 views7 pages

JS

This document provides an overview of JavaScript fundamentals, including variables, data types, operators, conditional statements, loops, functions, and user input. It explains variable declaration using var, let, and const, as well as primitive and non-primitive data types. Additionally, it includes examples of arithmetic, assignment, comparison, and logical operators, along with syntax for loops and functions.

Uploaded by

KRISHNA GUPTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

JS

This document provides an overview of JavaScript fundamentals, including variables, data types, operators, conditional statements, loops, functions, and user input. It explains variable declaration using var, let, and const, as well as primitive and non-primitive data types. Additionally, it includes examples of arithmetic, assignment, comparison, and logical operators, along with syntax for loops and functions.

Uploaded by

KRISHNA GUPTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Learn JS:

A) Variables and Data Types:

1) Console Log:
This is used when you want to write something on your console.

SYNTAX:
console.log("This a log statement");

2) Window Alert:
This creates a pop up alert on your window.

SYNTAX:
window.alert("This is a pop up");

3) Title Change:
This changes the document title

SYNTAX:
document.title = "Hello World"

4) Background color change:


This changes the background color through inline css

SYNTAX:
document.body.style.

1) Variables in JS:
Variables are containers for data.

Example:

fullName = "Tony Stark"


console.log(fullName);

--> Variable Rules:


1) Variable names are case sensitive; "a" &"A" is different.
2) Only letters , digits, underscore(_) and $ is allowed. (not even space)
3) Only a letter, underscore(_) or $ should be 1st character.
4) Reserved words should not be used as variable names.

2) let, const & var:

var : Variables can be re-declared & updated. A global scope variables.


let : Variables cannot be re-declared but can be updated. A block scope
variable.
const : Variable cannot be re-declared or updated. A block scope
variable.

## var is the least used of them all as it was used mostly used before
2016 and also mainly because of it's syntax and it's reexecutability.

Ex:

let fullName = "Tony Stark"


console.log(fullName);
Ex:

let fullName = "Tony Stark"


console.log(fullName);

##var is a global variable which means it's value will remain the same throughout
the codewhile conast and let are only applicable inside the block({.})

2) Data Types:
A) Primitive Data Types:

1) Numbers:
Ex:
let age = 24.555;
console.log(age);

2) String:
Ex:
let fullName = "Tony Stark";
console.log(fullName);

3) Boolean:
Ex:
let statement = True;
console.log(statement);

4) Undefined:
Ex:
let x ;
console.log(x);

5) Null:
Ex:
let x = null;
console.log(x);

6) Biglnt:
Ex:
let x = BigInt("123");
console.log(x);

7) Symbol:
Ex:
let x = Symbol("Hello!");
console.log(x);

B) Non Primitive Data Types{objects}:

Objects: Collection of values.


It is a key : value system
Ex:

const student = {
fullName : "Rahul Kumar",
age : 24,
cgpa : 8.2,
isPass : true,
};
console.log(student);
Practise Questions :

Create a const object called "product" to store information shown in the picture:

const pen = {
fullName : "Parker Jotter Standard CT Ball Pen (Black)",
rating : 4 + " Out of " + 5,
price : 270,
discount : 5 + "%",
};
console.log(pen);

B) Operators:
Used to perform some operation on data.

1) Arithmetic operators:
(+, -, *, /)

Ex :

let a = 5;
let b = 2;

console.log(a + b);

i) Modulus ---> %
ii) Exponentiation ---> **
iii) Increment ---> ++
iv) Decrement ---> --

2) Assignment Operators:
( =, +=, -=, *=, %=, **= )

3) Comparision Operators: (Always returns a boolean value)


(equal to : == , equal to & type : ===,)
(not equal to : != , Not equal to & type : !==,)
(>, >=, <, <=,)

4) Logical Operators: {Based on the logic gate principle}


{ Logical AND : &&
Logical OR : ||
Logical NOT : ! }

C) Conditional Statements:
To implement some condition in the code.

Ex:

let age = 45;

if(age > 18) {


console.log("You can drive")
}

else {
console.log("You cannot drive")
}
Ex:

let age = 19;

if(age>=18) {
console.log("You can drive")
} else {
console.log("You can not drive")
}

D) Loops:
It is used to perform repeated actions

SYNTAX EXAMPLE: (For Loop)

let age = 19;

if(age>=18) {
console.log("You can drive")
} else {
console.log("You can not drive")
}

SYNTAX EXAMPLE: (while loop)

let i = 1;

while (i < 6) {
console.log(i)
i++;
}

SYNTAX EXAMPLE: (for in loop)

let obj = {
name: "Krishna",
role: "Programmer",
company: "Code with Krishna"

for (const key in obj) {


console.log(key)

SYNTAX EXAMPLE: (for of loop)

let obj = {
name: "Krishna",
role: "Programmer",
company: "Code with Krishna"

for (const c of "KRISHNA") {


console.log(c)
}

SYNTAX EXAMPLE: (do while loop)

let i = 0;

do {
console.log(i);
i++;
} while (i < 6);

E) Functions:

SYNTAX EXAMPLE:

function nice(name) {
console.log("Hello " + name + " Nice to meet you")
console.log("Hello " + name + " Nice to meet you")
console.log("Hello " + name + " Nice to meet you")
console.log("Hello " + name + " Nice to meet you")
}

nice("YOU");
SYNTAX EXAMPLE:

function sum(a, b) {
return a + b
}

result = sum(3, 5);

## Arrow Function:

const func = (x)=>{


console.log("I am an arrow function " + x)
}

func(34);
func(56);
func(45);

>>>User input:

SYNTAX EXAMPLE:

fullName = prompt("Enter your name" + );


console.log("Your name is" + prompt);

Q Make a faulty calculator such that it is only wrong in 10% of the cases.
ans.

let num1 = parseFloat(prompt("Enter your First Number: "));


let Operation = prompt("Enter the operation you want: ");
let num2 = parseFloat(prompt("Enter your Second Number: "));
a = Math.random();

function faulty() {
if(Operation == "+") {
result = num1 - num2;
}
else if(Operation == "*") {
result = num1 + num2;
}
else if(Operation == "-") {
result = num1 / num2;
}
else if(Operation == "/") {
result = num1 ** num2;
}
}

function correct() {
if(Operation == "+") {
result = num1 + num2;
}
else if(Operation == "*") {
result = num1 * num2;
}
else if(Operation == "-") {
result = num1 - num2;
}
else if(Operation == "/") {
result = num1 / num2;
}
}

if (a < 0.1) {
faulty();
}
else {
correct();
}

window.alert("result" + result );

F) Strings:

You might also like