From 864c267c845039a4981bce0d71a9149169c6ef51 Mon Sep 17 00:00:00 2001 From: Shivam Kumar <2020uce0060@iitjammu.ac.in> Date: Thu, 24 Aug 2023 22:54:52 +0530 Subject: [PATCH] changes in node-1 --- demo.txt | 211 --------------------------------------------------- index.js | 32 ++------ lib.js | 6 +- package.json | 1 + 4 files changed, 13 insertions(+), 237 deletions(-) diff --git a/demo.txt b/demo.txt index eaf80ef..f82629d 100644 --- a/demo.txt +++ b/demo.txt @@ -69,214 +69,3 @@ Microsoft Logo © 2012-2023 Microsoft Privacy -Using TypeScript -Get Started -Download -Community -Playground -TSConfig Ref -Why TypeScript -Design -Code Samples - -Community -Get Help -Blog -GitHub Repo -Community Chat -@TypeScript -Stack Overflow -Web Repohghghghg -Navigated to TypeScript for the New ProgrammerSkip to main content -Get Started -TS for the New Programmer -TypeScript for JS Programmers -TS for Java/C# Programmers -TS for Functional Programmers -TypeScript Tooling in 5 minutes -Handbook -Reference -Tutorials -What's New -Declaration Files -JavaScript -Project Configuration -TypeScript for the New Programmer -Congratulations on choosing TypeScript as one of your first languages — you’re already making good decisions! - -You’ve probably already heard that TypeScript is a “flavor” or “variant” of JavaScript. The relationship between TypeScript (TS) and JavaScript (JS) is rather unique among modern programming languages, so learning more about this relationship will help you understand how TypeScript adds to JavaScript. - -What is JavaScript? A Brief History -JavaScript (also known as ECMAScript) started its life as a simple scripting language for browsers. At the time it was invented, it was expected to be used for short snippets of code embedded in a web page — writing more than a few dozen lines of code would have been somewhat unusual. Due to this, early web browsers executed such code pretty slowly. Over time, though, JS became more and more popular, and web developers started using it to create interactive experiences. - -Web browser developers responded to this increased JS usage by optimizing their execution engines (dynamic compilation) and extending what could be done with it (adding APIs), which in turn made web developers use it even more. On modern websites, your browser is frequently running applications that span hundreds of thousands of lines of code. This is long and gradual growth of “the web”, starting as a simple network of static pages, and evolving into a platform for rich applications of all kinds. - -More than this, JS has become popular enough to be used outside the context of browsers, such as implementing JS servers using node.js. The “run anywhere” nature of JS makes it an attractive choice for cross-platform development. There are many developers these days that use only JavaScript to program their entire stack! - -To summarize, we have a language that was designed for quick uses, and then grew to a full-fledged tool to write applications with millions of lines. Every language has its own quirks — oddities and surprises, and JavaScript’s humble beginning makes it have many of these. Some examples: - -JavaScript’s equality operator (==) coerces its arguments, leading to unexpected behavior: - -if ("" == 0) { - // It is! But why?? -} -if (1 < x < 3) { - // True for *any* value of x! -} -JavaScript also allows accessing properties which aren’t present: - -const obj = { width: 10, height: 15 }; -// Why is this NaN? Spelling is hard! -const area = obj.width * obj.heigth; -Most programming languages would throw an error when these sorts of errors occur, some would do so during compilation — before any code is running. When writing small programs, such quirks are annoying but manageable; when writing applications with hundreds or thousands of lines of code, these constant surprises are a serious problem. - -TypeScript: A Static Type Checker -We said earlier that some languages wouldn’t allow those buggy programs to run at all. Detecting errors in code without running it is referred to as static checking. Determining what’s an error and what’s not based on the kinds of values being operated on is known as static type checking. - -TypeScript checks a program for errors before execution, and does so based on the kinds of values, it’s a static type checker. For example, the last example above has an error because of the type of obj. Here’s the error TypeScript found: - -const obj = { width: 10, height: 15 }; -const area = obj.width * obj.heigth; -Property 'heigth' does not exist on type '{ width: number; height: number; }'. Did you mean 'height'? -Try -A Typed Superset of JavaScript -How does TypeScript relate to JavaScript, though? - -Syntax -TypeScript is a language that is a superset of JavaScript: JS syntax is therefore legal TS. Syntax refers to the way we write text to form a program. For example, this code has a syntax error because it’s missing a ): - -let a = (4 -')' expected. -Try -TypeScript doesn’t consider any JavaScript code to be an error because of its syntax. This means you can take any working JavaScript code and put it in a TypeScript file without worrying about exactly how it is written. - -Types -However, TypeScript is a typed superset, meaning that it adds rules about how different kinds of values can be used. The earlier error about obj.heigth was not a syntax error: it is an error of using some kind of value (a type) in an incorrect way. - -As another example, this is JavaScript code that you can run in your browser, and it will log a value: - -console.log(4 / []); -This syntactically-legal program logs Infinity. TypeScript, though, considers division of number by an array to be a nonsensical operation, and will issue an error: - -console.log(4 / []); -The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. -Try -It’s possible you really did intend to divide a number by an array, perhaps just to see what happens, but most of the time, though, this is a programming mistake. TypeScript’s type checker is designed to allow correct programs through while still catching as many common errors as possible. (Later, we’ll learn about settings you can use to configure how strictly TypeScript checks your code.) - -If you move some code from a JavaScript file to a TypeScript file, you might see type errors depending on how the code is written. These may be legitimate problems with the code, or TypeScript being overly conservative. Throughout this guide we’ll demonstrate how to add various TypeScript syntax to eliminate such errors. - -Runtime Behavior -TypeScript is also a programming language that preserves the runtime behavior of JavaScript. For example, dividing by zero in JavaScript produces Infinity instead of throwing a runtime exception. As a principle, TypeScript never changes the runtime behavior of JavaScript code. - -This means that if you move code from JavaScript to TypeScript, it is guaranteed to run the same way, even if TypeScript thinks that the code has type errors. - -Keeping the same runtime behavior as JavaScript is a foundational promise of TypeScript because it means you can easily transition between the two languages without worrying about subtle differences that might make your program stop working. - -Erased Types -Roughly speaking, once TypeScript’s compiler is done with checking your code, it erases the types to produce the resulting “compiled” code. This means that once your code is compiled, the resulting plain JS code has no type information. - -This also means that TypeScript never changes the behavior of your program based on the types it inferred. The bottom line is that while you might see type errors during compilation, the type system itself has no bearing on how your program works when it runs. - -Finally, TypeScript doesn’t provide any additional runtime libraries. Your programs will use the same standard library (or external libraries) as JavaScript programs, so there’s no additional TypeScript-specific framework to learn. - -Learning JavaScript and TypeScript -We frequently see the question “Should I learn JavaScript or TypeScript?“. - -The answer is that you can’t learn TypeScript without learning JavaScript! TypeScript shares syntax and runtime behavior with JavaScript, so anything you learn about JavaScript is helping you learn TypeScript at the same time. - -There are many, many resources available for programmers to learn JavaScript; you should not ignore these resources if you’re writing TypeScript. For example, there are about 20 times more StackOverflow questions tagged javascript than typescript, but all of the javascript questions also apply to TypeScript. - -If you find yourself searching for something like “how to sort a list in TypeScript”, remember: TypeScript is JavaScript’s runtime with a compile-time type checker. The way you sort a list in TypeScript is the same way you do so in JavaScript. If you find a resource that uses TypeScript directly, that’s great too, but don’t limit yourself to thinking you need TypeScript-specific answers for everyday questions about how to accomplish runtime tasks. - -Next Steps -This was a brief overview of the syntax and tools used in everyday TypeScript. From here, you can: - -Learn some of the JavaScript fundamentals, we recommend either: - -Microsoft’s JavaScript Resources or -JavaScript guide at the Mozilla Web Docs -Continue to TypeScript for JavaScript Programmers - -Read the full Handbook from start to finish (30m) - -Explore the Playground examples - -On this page -What is JavaScript? A Brief History -TypeScript: A Static Type Checker -A Typed Superset of JavaScript -Learning JavaScript and TypeScript -Next Steps -Is this page helpful? -YesNo -The TypeScript docs are an open source project. Help us improve these pages by sending a Pull Request ❤ - -Contributors to this page: -OTOrta Therox (19)EBEli Barzilay (2)谭谭九鼎 (1)LLalit (1)Ggraue70 (1)2+ -Last updated: Feb 07, 2023 - -This page loaded in 1.285 seconds. - -Customize -Site Colours: - - -System -Code Font: - - -Cascadia -Popular Documentation Pages -Everyday Types -All of the common types in TypeScript - -Creating Types from Types -Techniques to make more elegant types - -More on Functions -How to provide types to functions in JavaScript - -More on Objects -How to provide a type shape to JavaScript objects - -Narrowing -How TypeScript infers types based on runtime behavior - -Variable Declarations -How to create and type JavaScript variables - -TypeScript in 5 minutes -An overview of building a TypeScript web app - -TSConfig Options -All the configuration options for a project - -Classes -How to provide types to JavaScript ES6 classes - -Made with ♥ in Redmond, Boston, SF & Dublin - -Microsoft Logo -© 2012-2023 Microsoft -Privacy - - -What is JavaScript? A Brief History -JavaScript (also known as ECMAScript) started its life as a simple scripting language for browsers. At the time it was invented, it was expected to be used for short snippets of code embedded in a web page — writing more than a few dozen lines of code would have been somewhat unusual. Due to this, early web browsers executed such code pretty slowly. Over time, though, JS became more and more popular, and web developers started using it to create interactive experiences. - -Web browser developers responded to this increased JS usage by optimizing their execution engines (dynamic compilation) and extending what could be done with it (adding APIs), which in turn made web developers use it even more. On modern websites, your browser is frequently running applications that span hundreds of thousands of lines of code. This is long and gradual growth of “the web”, starting as a simple network of static pages, and evolving into a platform for rich applications of all kinds. - -More than this, JS has become popular enough to be used outside the context of browsers, such as implementing JS servers using node.js. The “run anywhere” nature of JS makes it an attractive choice for cross-platform development. There are many developers these days that use only JavaScript to program their entire stack! - -To summarize, we have a language that was designed for quick uses, and then grew to a full-fledged tool to write applications with millions of lines. Every language has its own quirks — oddities and surprises, and JavaScript’s humble beginning makes it have many of these. Some examples: - -JavaScript’s equality operator (==) coerces its arguments, leading to unexpected behavior: - -if ("" == 0) { - // It is! But why?? -} -if (1 < x < 3) { - // True for *any* value of x! -} -JavaScript also allows accessing properties which aren’t present: - diff --git a/index.js b/index.js index d3647ef..e782b82 100644 --- a/index.js +++ b/index.js @@ -1,33 +1,17 @@ -const lib = require('./lib.js') -const express = require('express'); +import * as lib from "./lib.js"; +import * as fs from "fs"; -console.log('hello ') - -const server = express(); -server.listen(8080); - - - - - - - - -// import {sum,diff} from './lib.js'; -// const fs = require('fs'); - -// const t1 = performance.now(); +// console.log('hello ') +//Synchronous +// const txt = fs.readFileSync('demo.txt'); +// console.log(txt); // const txt = fs.readFileSync('demo.txt','utf-8'); +// console.log(txt); +//Ascynchronous // fs.readFile('demo.txt','utf-8',(err, txt)=>{ // console.log(txt) // }); -// console.log(txt); - // console.log(lib.sum(4,5),lib.diff(3,6)) -// const t2 = performance.now(); -// console.log(t2-t1); -// const a = 5; - diff --git a/lib.js b/lib.js index bb1a47a..b3d8755 100644 --- a/lib.js +++ b/lib.js @@ -1,8 +1,10 @@ -exports.sum =(a,b)=>{ +const sum =(a,b)=>{ return a+b; } -exports.diff =(a,b)=>{ +const diff =(a,b)=>{ return a-b; } +export {sum,diff}; + diff --git a/package.json b/package.json index 3769927..c35d502 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "node-example", + "type": "module", "version": "1.0.0", "description": "a example of node", "main": "index.js",