8000 initial · Himanxusharma/JavaScript@4856b6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4856b6f

Browse files
committed
initial
1 parent 19a01ef commit 4856b6f

File tree

2 files changed

+269
-19
lines changed

2 files changed

+269
-19
lines changed

Fundamentals/console.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

Fundamentals/variables.js

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
//Console
2+
console.log('Hello World');
3+
console.log(123);
4+
console.log(true);
5+
var greeting = 'Hello';
6+
console.log(greeting);
7+
console.log([1, 2, 3, 4]);
8+
console.log({ a: 1, b: 2 });
9+
console.table({ a: 1, b: 2 });
10+
console.error('This is some error');
11+
// console.clear();
12+
console.warn('This is a warning');
13+
console.time('Hello');
14+
console.log('Hello World');
15+
console.log('Hello World');
16+
console.log('Hello World');
17+
console.log('Hello World');
18+
console.timeEnd('Hello');
19+
20+
21+
22+
23+
24+
125
var name="John";
226
console.log(name);
327
name="Steve Smith";
@@ -110,3 +134,248 @@ val=String(true);
110134
val=String(new Date());
111135
// Array to string
112136
val=String([1,2,3,4]);
137+
138+
139+
// toString()
140+
val=(5).toString();
141+
val=(true).toString();
142+
143+
// String to number
144+
val=Number('5');
145+
val=Number(true);
146+
val=Number(false);
147+
val=Number(null);
148+
console.log(val);
149+
console.log(typeof val);
150+
val=Number('hello');
151+
val=Number([1,2,3]);
152+
console.log(val);
153+
154+
155+
val=parseInt('100.30');
156+
val=parseFloat('100.30');
157+
158+
159+
// Output
160+
console.log(val);
161+
console.log(typeof val);
162+
console.log(val.length);
163+
console.log(val.toFixed(2));
164+
165+
val= new Date();
166+
val= val.toString();
167+
console.log(val);
168+
169+
// Type Coercion
170+
const val1=String(5);
171+
const val2=6;
172+
const sum=Number(val1+val2);
173+
console.log(sum);
174+
console.log(typeof sum);
175+
176+
177+
// Numbers & The Math Object
178+
const num1=100;
179+
const num2=50;
180+
let val3;
181+
182+
183+
// Simple math with numbers
184+
val3=num1+num2;
185+
val3=num1-num2;
186+
val3=num1*num2;
187+
val3=num1/num2;
188+
val3=num1%num2;
189+
190+
191+
// Math Object
192+
val3=Math.PI;
193+
val3=Math.E;
194+
val3=Math.round(2.8);
195+
val3=Math.ceil(2.4);
196+
val3=Math.floor(2.8);
197+
val3=Math.sqrt(64);
198+
val3=Math.abs(-3);
199+
val3=Math.pow(8,2);
200+
val3=Math.min(2,33,4,1,55,6,3,-2);
201+
val3=Math.max(2,33,4,1,55,6,3,-2);
202+
val3=Math.random();
203+
val3=Math.floor(Math.random()*20+1);
204+
205+
206+
console.log(val3);
207+
208+
209+
// String Methods & Concatenation
210+
const firstName1='William';
211+
const lastName1='Johnson';
212+
const age1=36;
213+
const str='Hello there my name is Brad';
214+
const tags='web design,web development,programming';
215+
216+
217+
let val4;
218+
219+
val4=firstName1+lastName1;
220+
221+
// Concatenation
222+
val4=firstName1+' '+lastName1;
223+
224+
// Append
225+
val4='Brad ';
226+
val4+='Traversy';
227+
228+
val4='Hello, my name is '+firstName1+' and I am '+age1;
229+
230+
// Escaping
231+
val4='That\'s awesome, I can\'t wait';
232+
233+
// Length
234+
val4=firstName1.length;
235+
236+
// concat()
237+
val4=firstName1.concat(' ',lastName1);
238+
239+
// Change case
240+
val4=firstName1.toUpperCase();
241+
val4=firstName1.toLowerCase();
242+
243+
val4=firstName1[2];
244+
245+
// indexOf()
246+
val4=firstName1.indexOf('l');
247+
val4=firstName1.lastIndexOf('l');
248+
249+
// charAt()
250+
val4=firstName1.charAt('2');
251+
// Get last char
252+
val4=firstName1.charAt(firstName1.length-1);
253+
254+
// substring()
255+
val4=firstName1.substring(0,4);
256+
257+
// slice()
258+
val4=firstName1.slice(0,4);
259+
val4=firstName1.slice(-3);
260+
261+
// split()
262+
val4=str.split(' ');
263+
val4=tags.split(',');
264+
// replace()
265+
val4=str.replace('Brad','Jack');
266+
267+
// includes()
268+
val4=str.includes('Hello');
269+
270+
console.log(val4);
271+
272+
273+
// Template Literals
274+
const name3='John';
275+
const age3=30;
276+
const job='Web Developer';
277+
const city='Miami';
278+
let html;
279+
280+
// Without template strings (es5)
281+
html='<ul><li>Name: '+name3+'</li><li>Age: '+age3+'</li><li>Job: '+job+'</li><li>City: '+city+'</li></ul>';
282+
283+
html='<ul>'+
284+
'<li>Name: '+name3+'</li>'+
285+
'<li>Age: '+age3+'</li>'+
286+
'<li>Job: '+job+'</li>'+
287+
'<li>City: '+city+'</li>'+
288+
'</ul>';
289+
290+
function hello(){
291+
return 'hello';
292+
}
293+
294+
// With template strings (es6)
295+
html=`
296+
<ul>
297+
<li>Name: ${name3}</li>
298+
<li>Age: ${age3}</li>
299+
<li>Job: ${job}</li>
300+
<li>City: ${city}</li>
301+
<li>${2+2}</li>
302+
<li>${hello()}</li>
303+
<li>${age3>30 ? 'Over 30' : 'Under 30'}</li>
304+
</ul>
305+
`;
306+
307+
document.body.innerHTML=html;
308+
309+
310+
// Arrays & Array Methods
311+
// Create some arrays
312+
const numbers1=[43,56,33,23,44,36,5];
313+
const numbers2=new Array(22,45,33,76,54);
314+
const fruit=['Apple','Banana','Orange','Pear'];
315+
const mixed=[22,'Hello', F438 true,undefined,null,{a:1,b:1},new Date()];
316+
let val5;
317+
318+
// Get array length
319+
val5=numbers1.length;
320+
// Check if is array
321+
val5=Array.isArray(numbers1);
322+
// Get single value
323+
val5=numbers1[3];
324+
val5=numbers1[0];
325+
// Insert into array
326+
numbers1[2]=100;
327+
// Find index of value
328+
val5=numbers1.indexOf(36);
329+
330+
// MUTATING ARRAYS
331+
// Add on to end
332+
numbers1.push(250);
333+
// Add on to front
334+
numbers1.unshift(120);
335+
// Take off from end
336+
numbers1.pop();
337+
// Take off from front
338+
numbers1.shift();
339+
// Splice values
340+
numbers1.splice(1,3);
341+
// Reverse
342+
numbers1.reverse();
343+
344+
// Concatenate array
345+
val5=numbers1.concat(numbers2);
346+
347+
// Sorting arrays
348+
val5=fruit.sort();
349+
val5=numbers1.sort();
350+
351+
// Use the "compare function"
352+
val5=numbers1.sort(function(x,y){
353+
return x-y;
354+
}
355+
);
356+
357+
// Reverse sort
358+
val5=numbers1.sort(function(x,y){
359+
return y-x;
360+
}
361+
);
362+
363+
// Find
364+
function under50(num){
365+
return num<50;
366+
}
367+
368+
val5=numbers1.find(under50);
369+
370+
console.log(numbers1);
371+
console.log(val5);
372+
373+
374+
// Object Literals
375+
const person1={
376+
firstName:'Steve',
377+
lastName:'Smith',
378+
age:30,
379+
email:''}
380+
381+

0 commit comments

Comments
 (0)
0