0% found this document useful (0 votes)
2 views

Java functions

The document provides an extensive overview of JavaScript concepts, including functions, data types, operators, conditionals, loops, classes, and predefined functions. It includes examples of each concept, demonstrating their usage and syntax. Key topics covered are function definitions, return values, data types (string, integer, boolean, etc.), array manipulations, control flow statements, and object-oriented programming principles.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java functions

The document provides an extensive overview of JavaScript concepts, including functions, data types, operators, conditionals, loops, classes, and predefined functions. It includes examples of each concept, demonstrating their usage and syntax. Key topics covered are function definitions, return values, data types (string, integer, boolean, etc.), array manipulations, control flow statements, and object-oriented programming principles.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

TOPICS EXAMPLES:

1.FUNCTION:A set of statements that performs a task or caluculates a value but for
a procedure to qualify as function.

**Function examples with string format:

a.//function with out parameters

function name(){
let a="vali"
let b="sahab"
let c="yasaswi"
console.log(a,b,c)
}
name()

b.//function with parameters

function add(a,b,c){
console.log(a,b,c)
}
add("dell","hp","lenovo")

**Function examples with integer formate or integer type:

a.//function with out parameters


function num(){
let a=4
let b=7
let c=2
console.log(a+b-c)
}
num()

b.//function with parameters


function add(a,b,c){
console.log(a+b+c)
}
add(74,64,47)

2.RETURN FUNCTION:A return is a value that a function returns to the calling script
or function when it completes its task. A return value can be any one of the four
variable types: handle, integer, object, or string.

**Return function with & with out parameters in integer type:


a.//function with out parameters
function num(){
let a=4
let b=7
let c=2
return(a+b+c)
}
x=num()
console.log(x)

b.//function with parameters


function add(a,b,c){
return(a*b*c)
}
y=add(74,64,47)
console.log(y)

**Return function with & with out parameters in string type:


a.//function with out parameters
function laptop(){
let a="dell"
let b="hp"
let c="lenovo"
return(a+b+c)
}
x=laptop()
console.log(x)

b.//function with parameters


function bike(a,b,c){
return(a+b+c)
}
y=bike("java","enfield","pulsar")
console.log(y)

3.DATA TYPES:A data type is an attribute associated with a piece of data that tells
a computer system how to interpret its value.

**Data type Examples:

//String :
a.var str1="hi world"
var str2="hello world"
console.log("str1","str2")

//Integer :
b.var num1=5
var num2=8
console.log(num1+num2)

//Boolean :The boolean (not Boolean) is a primitive data type in JavaScript. It can
have only two values: true or false. It is useful in controlling program flow using
conditional statements like if else, switch, while loop, etc.
c.var a=true
var b=false
console.log(a,b)

//Undefined :
d.var a= undefined
var b= undefined
console.log(b,a)

//Null :In JavaScript, null is a special value that represents an empty or unknown
value. For example, let number = null; The code above suggests that the number
variable is empty at the moment and may have a value later.

e.var a= null
var b= null
console.log(b,a)

f.Object :An object is a collection of properties, and a property is an association


between a name (or key) and a value.
var result={
yasaswi:85,
vali:95,
shahab:100,
faiz:90
}
console.log (result)

4.Array:In JavaScript, an array is one of the most commonly used data types. It
stores multiple values and elements in one variable. These values can be of any
data type — meaning you can store a string, number, boolean, and other data types
in one variable.

*var arr=[54,64,74,84,94]
console.log(arr)

output:[54,64,74,84,94]

*var add=["yash",50,"vali",60,"shaik"]
console.log(add)

output:[ 'yash', 50, 'vali', 60, 'shaik' ]

//To find the length in array:

*var add=["yash",50,"vali",60,"shaik"]
console.log(add.length)

==>how to remove duplicate items in array ?


by using Set and spreadoperater (...)

let name = ["yash","vali","faiz","yash","rahul",5,4,3,8,true];


console.log(name)
let newArray = [...new Set(name)];
console.log(newArray)

**POP:To remove last element in array we can use pop.

var add=["yash",5,"shahab",4,"valli",9,6]
console.log(add)
add.pop();
console.log(add)

output:[ 'yash', 5, 'shahab', 4, 'valli', 9, 6 ]


[ 'yash', 5, 'shahab', 4, 'valli', 9 ]

**Push:The push() method adds new items to the end of an array.

let Name = ["black","white","blue","red"]


console.log(Name)
Name.push("orange")
console.log(Name)

output:[ 'black', 'white', 'blue', 'red' ]


[ 'black', 'white', 'blue', 'red', 'orange' ]
**Shift:To remove first element in array we i'll use shift.

let Name = ["Haier","Sony","Lg","Bpl"]


console.log(Name.length)
Name.shift()
console.log(Name)

output:4
[ 'Sony', 'Lg', 'Bpl' ]

**Unshift:To add first element in array we i'll use "Unshift"

let Name = ["Haier",2,6,8,"Sony",7,"Lg",5,"Bpl"]


console.log(Name.length)
Name.unshift("Philips",1)
console.log(Name)

output:9
[
'Philips', 1,
'Haier', 2,
6, 8,
'Sony', 7,
'Lg', 5,
'Bpl'
]

**Newlength:To add new length in an array we i'll use the "Newlength".

let Name = ["Haier",2,6,8,"Sony",7,"Lg",5,"Bpl"];


console.log("length of array",Name.length)
const newlen=Name.unshift("Realme")
console.log("length of the array",newlen)
console.log(Name)

output:length of array 9
length of the array 10
[
'Realme', 'Haier',
2, 6,
8, 'Sony',
7, 'Lg',
5, 'Bpl'
]

5.operaters :
// arthematic operaters :Arithmetic operators are the fundamental mathematical
operators used in programming to carry out calculations on numerical data. In
JavaScript, arithmetic operators can be utilized to perform addition, subtraction,
multiplication, division, and modulo operations.

var a=40
var b=30
console.log("The value of a+b is",a+b)
var x=80
var y=20
console.log("The value of x-y is",x-y)
console.log("Tha value of x*y is",x*y)
console.log("The value of x/y is",x/y)

// assign operaters :The assignment ( = ) operator is used to assign a value to a


variable or property. The assignment expression itself has a value, which is the
assigned value. This allows multiple assignments to be chained in order to assign a
single value to multiple variables.

var b = 10;
b += 2;
console.log(b)

output:12

// comparrision operater :The "===" operator compares the variable's value and data
types. It will return true if the value and data type are the same. For example, if
we compare a===3 and a==="3", then in the first case, it will return true if the
assigned value of a is 3, and in the second case, it will return false.

var x=50
var y=500
console.log(x==y)
console.log(x>y)
console.log(x<y)

// logical & operater :The logical AND operator ( && ) returns true if both
operands are true and returns false otherwise.

console.log(true && true)


console.log(true && false)
console.log(false && false)
console.log(false && true)

// logical NOT operater :The logical NOT operator flips the value of a boolean. If
the value is true, the NOT operator returns false. If the value is false, the NOT
operator returns true.

console.log(!false)
console.log(!true)

6.Condition
*If statement :The IF statement works by checking the expression to see whether a
condition is met and returns a value based on the output obtained.

var age=25;
if (age>=18){
console.log("yasaswi adultperson")
}

*else If :"else if" is a conditional statement that allows you to specify multiple
conditions to be evaluated in a sequence.

var mandals =15


if (mandals <11){
console.log("ATP has 15 mandals")
}
else{
console.log("hindupur having 10 mandals")
}

7.Loops :Loops are used in JavaScript to perform repeated tasks based on a


condition. Conditions typically return true or false . A loop will continue running
until the defined condition returns false .

//For loop:A "For" Loop is used to repeat a specific block of code a known number
of times.

*for(let i=0; i<=100; i++) {


console.log(i)
}

//While loop:A while loop is a loop that continues to run and execute a while
statement as long as a predetermined condition holds true.

*let a = 1;
while(a <= 50){
console.log(a)
a++;
}

*Even numbers by using while loop:

let i = 0;
while(i <= 20){
console.log(i)
i=i+2;
}

*odd numbers by using while loop:

let x = 1;
while(x <= 20){
console.log(x)
x=x+2;
}

//Do while:The do... while statement creates a loop that executes a specified
statement until the test condition evaluates to false.

*Decrement:

let a=20;
do{
console.log(a)
a--
}
while(a>=10)

*Increment:

let i=0;
do{
console.log(i)
i++
}
while(i<=20)

8.Break statement:Break statement stops the entire process of the loop.

*var arr=[1,2,3,4,5,6,7,8,9]
for(let i=0;i<arr.length;i++){
if(i==5){
break;
}
console.log(arr[i])
}

9.Continue statement:Continue statement only stops the current iteration of the


loop.

var array1=[1,2,3,4,5,6,7,8,9,1,2,3,4,5]
console.log("The number of elements inside the array",array1.length)
for(let i=0;i<24;i++){
if(i==5){
continue;
}
console.log(array1[i])
}

10.Class:class function as templates for creating objects in object-oriented


programming, encapsulating both data and behaviour.

**class State{
district1(){
console.log("clocktower in anantapur")
}
district2(){
console.log("kondareddyburju in kurnool")
}
}
x=new State();
x.district1();
x.district2();

output:clocktower in anantapur
kondareddyburju in kurnool

**class India{

Indianforces(Army,Airforce,Navy){

if(Army<3,Airforce<3,Navy<3){

console.log("Indianforces one of the powerfull miltilary in the world")

console.log("Top 3 forces in the world")

else{
console.log("Indianforces one of the powerfull miltilary in the world
")

console.log("Top 2 forces in world")

obj=new India
obj.Indianforces(2,2,2)

output:Indianforces one of the powerfull miltilary in the world


Top 3 forces in the world

**class constructor:A constructor is a special function that creates and


initializes an object instance of a class.

class Movies {
spirit=500
constructor(rrr,bahuballi,projectk){
console.log(rrr,bahuballi,projectk)
console.log(this.spirit)
}
devara(){
console.log(750)
}
}
obj=new Movies(1000,2000,3000)
obj.devara()

out put:1000 2000 3000


500
750

11.Switch statement:A switch statement compares the value of an expression to


multiple cases.
(or)
The switch statement executes a block of code depending on different cases.

*Switch statement in string type:

let name="vali"
switch(name) {
case "vali":
console.log("The name of the student is vali")
console.log("he scored in exams 100 percent")
break;
case "shahab":
console.log("The name of the student is shahab")
console.log("he scored in exams 110 percent")
break;
case "yash":
console.log("The name of the student is yash")
console.log("he scored in exams 80 percent")
break;
}
output:The name of the student is vali
he scored in exams 100 percent

*Switch statement in integer type:

let a=20
switch(a) {
case 10:
console.log("case 10 is a even number")
break;
case 20:
console.log("case 20 is a odd number")
break;
case 30:
console.log("case 30 is a just a number")
break;
}

output:case 20 is a odd number

12.setTimeout:You specify a callback function to execute later, and a value


expressing how later you want it to run, in milliseconds.

setTimeout(function sum(){
console.log(100+50+30+80)
},5000)

output:260

13.setInterval:setInterval is a function similar to setTimeout , with a difference:


instead of running the callback function once, it will run it forever, at the
specific time interval you specify.

setInterval(function sum(){
console.log(60+50+80+70)
},6000)

output:260

14:Predefine:JavaScript has several "top-level" functions predefined in the


language eval , isNan , Number , String , parseInt, parseFloat , escape ,
unescape , taint , and untaint . For more information on all of these functions,
see the JavaScript Reference.

a.eval:The eval() method evaluates or executes an argument.


If the argument is an expression, eval() evaluates the expression. If the argument
is one or more JavaScript statements, eval() executes the statements.

let a=20;
let b=40;
let exam = "a * b";
console.log (result=eval(exam));

output:800

b.isNaN:isNaN() returns true if a number is Not-a-Number. In other words: isNaN()


converts the value to a number before testing it.
let c=456;
console.log(isNaN("c"));

output:true

c.parseInt:The parseInt function converts its first argument to a string, parses


that string, then returns an integer or NaN.

let a=2
let b=5
let c=9
console.log(parseInt(b));

output:5

d.parseFloat:The parseFloat function converts its first argument to a string,


parses that string as a decimal number literal, then returns a number or NaN. The
number syntax it accepts can be summarized as.

let a=22.5
let b=52.659
let c=9.0
console.log(parseFloat(b));

output:52.659

e.isFinite:isFinite() static method determines whether the passed value is a finite


number.

let a= 8523
console.log(isFinite(a))

output:true

**Reverse:

let i=0;
while(i<=10){
console.log(i)
i++;
}

You might also like